Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest Manager] Define custom errors by extending Error #69966

Merged
merged 3 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions x-pack/plugins/ingest_manager/server/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

/* eslint-disable max-classes-per-file */
export class IngestManagerError extends Error {
public type: IngestManagerErrorType;
public message: string;

constructor(type: IngestManagerErrorType, message: string) {
constructor(message?: string) {
super(message);
this.type = type;
this.message = message;
this.name = this.constructor.name; // for stack traces
}
}

export const getHTTPResponseCode = (error: IngestManagerError): number => {
switch (error.type) {
case IngestManagerErrorType.RegistryError:
return 502; // Bad Gateway
default:
return 400; // Bad Request
if (error instanceof RegistryError) {
return 502; // Bad Gateway
} else {
return 400; // Bad Request
}
};

export enum IngestManagerErrorType {
RegistryError,
}
export class RegistryError extends IngestManagerError {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to extend IngestManagerError here so that it gets the benefits of the stack trace naming from https://github.com/elastic/kibana/pull/69966/files#diff-305aab707fa2746fa91c6bec9e78c0b7R11 and remain an IngestManagerError

However, even if it did extend Error we could make a type like

type IngestManagerErrorType = IngestManagerError | RegistryError

This is where my mind is heading now. Files will define/export Errors where they are used (or perhaps lifted to common or server), and then those can be imported and combined into other types

Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@

import fetch, { Response } from 'node-fetch';
import { streamToString } from './streams';
import { IngestManagerError, IngestManagerErrorType } from '../../../errors';
import { RegistryError } from '../../../errors';

export async function getResponse(url: string): Promise<Response> {
try {
const response = await fetch(url);
if (response.ok) {
return response;
} else {
throw new IngestManagerError(
IngestManagerErrorType.RegistryError,
throw new RegistryError(
`Error connecting to package registry at ${url}: ${response.statusText}`
);
}
} catch (e) {
throw new IngestManagerError(
IngestManagerErrorType.RegistryError,
`Error connecting to package registry at ${url}: ${e.message}`
);
throw new RegistryError(`Error connecting to package registry at ${url}: ${e.message}`);
}
}

Expand Down