Skip to content

Commit d67910a

Browse files
author
John Schulz
committed
Make custom errors by extending Error
1 parent 2685654 commit d67910a

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

x-pack/plugins/ingest_manager/server/errors.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,20 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
/* eslint-disable max-classes-per-file */
78
export class IngestManagerError extends Error {
8-
public type: IngestManagerErrorType;
9-
public message: string;
10-
11-
constructor(type: IngestManagerErrorType, message: string) {
9+
constructor(message?: string) {
1210
super(message);
13-
this.type = type;
14-
this.message = message;
11+
this.name = this.constructor.name; // for stack traces
1512
}
1613
}
1714

1815
export const getHTTPResponseCode = (error: IngestManagerError): number => {
19-
switch (error.type) {
20-
case IngestManagerErrorType.RegistryError:
21-
return 502; // Bad Gateway
22-
default:
23-
return 400; // Bad Request
16+
if (error instanceof RegistryError) {
17+
return 502; // Bad Gateway
18+
} else {
19+
return 400; // Bad Request
2420
}
2521
};
2622

27-
export enum IngestManagerErrorType {
28-
RegistryError,
29-
}
23+
export class RegistryError extends IngestManagerError {}

x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@
66

77
import fetch, { Response } from 'node-fetch';
88
import { streamToString } from './streams';
9-
import { IngestManagerError, IngestManagerErrorType } from '../../../errors';
9+
import { RegistryError } from '../../../errors';
1010

1111
export async function getResponse(url: string): Promise<Response> {
1212
try {
1313
const response = await fetch(url);
1414
if (response.ok) {
1515
return response;
1616
} else {
17-
throw new IngestManagerError(
18-
IngestManagerErrorType.RegistryError,
17+
throw new RegistryError(
1918
`Error connecting to package registry at ${url}: ${response.statusText}`
2019
);
2120
}
2221
} catch (e) {
23-
throw new IngestManagerError(
24-
IngestManagerErrorType.RegistryError,
25-
`Error connecting to package registry at ${url}: ${e.message}`
26-
);
22+
throw new RegistryError(`Error connecting to package registry at ${url}: ${e.message}`);
2723
}
2824
}
2925

0 commit comments

Comments
 (0)