Skip to content

Commit

Permalink
refactoring error files
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Sep 9, 2020
1 parent 9027b2a commit 430dbdb
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Boom from 'boom';

import { extractErrorMessage, MLHttpFetchError, MLResponseError, EsErrorBody } from './errors';
import { extractErrorMessage, MLHttpFetchError, MLResponseError, EsErrorBody } from './index';

describe('ML - error message utils', () => {
describe('extractErrorMessage', () => {
Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/ml/common/util/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { MLRequestFailure } from './request_error';
export { extractErrorMessage, extractErrorProperties } from './process_errors';
export {
ErrorType,
EsErrorBody,
EsErrorRootCause,
MLErrorObject,
MLHttpFetchError,
MLResponseError,
isBoomError,
isErrorString,
isEsErrorBody,
isMLResponseError,
} from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { HttpFetchError } from 'kibana/public';
import Boom from 'boom';

export interface EsErrorRootCause {
type: string;
reason: string;
}

export interface EsErrorBody {
error: {
root_cause?: EsErrorRootCause[];
type: string;
reason: string;
};
status: number;
}

export interface MLResponseError {
statusCode: number;
error: string;
message: string;
attributes?: {
body: EsErrorBody;
};
}

export interface MLHttpFetchError<T> extends HttpFetchError {
body: T;
}

export type ErrorType = MLHttpFetchError<MLResponseError> | EsErrorBody | Boom | string | undefined;

function isEsErrorBody(error: any): error is EsErrorBody {
return error && error.error?.reason !== undefined;
}

function isErrorString(error: any): error is string {
return typeof error === 'string';
}

function isMLResponseError(error: any): error is MLResponseError {
return typeof error.body === 'object' && 'message' in error.body;
}

function isBoomError(error: any): error is Boom {
return error.isBoom === true;
}

export interface MLErrorObject {
message: string;
statusCode?: number;
fullError?: EsErrorBody;
}
import {
ErrorType,
MLErrorObject,
isBoomError,
isErrorString,
isEsErrorBody,
isMLResponseError,
} from './types';

export const extractErrorProperties = (error: ErrorType): MLErrorObject => {
// extract properties of the error object from within the response error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { KbnError } from '../../../../../../src/plugins/kibana_utils/public';
import { MLErrorObject, ErrorType } from './types';

export class MLRequestFailure extends KbnError {
// takes an Error object and and optional response object

constructor(error: any, resp: any) {
export class MLRequestFailure extends Error {
constructor(error: MLErrorObject, resp: ErrorType) {
super(error.message);
Object.setPrototypeOf(this, new.target.prototype);

if (typeof resp === 'object') {
if (typeof resp !== 'string' && typeof resp !== 'undefined') {
if ('body' in resp) {
this.stack = JSON.stringify(resp.body, null, 2);
} else {
Expand Down
59 changes: 59 additions & 0 deletions x-pack/plugins/ml/common/util/errors/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { HttpFetchError } from 'kibana/public';
import Boom from 'boom';

export interface EsErrorRootCause {
type: string;
reason: string;
}

export interface EsErrorBody {
error: {
root_cause?: EsErrorRootCause[];
type: string;
reason: string;
};
status: number;
}

export interface MLResponseError {
statusCode: number;
error: string;
message: string;
attributes?: {
body: EsErrorBody;
};
}

export interface MLErrorObject {
message: string;
statusCode?: number;
fullError?: EsErrorBody;
}

export interface MLHttpFetchError<T> extends HttpFetchError {
body: T;
}

export type ErrorType = MLHttpFetchError<MLResponseError> | EsErrorBody | Boom | string | undefined;

export function isEsErrorBody(error: any): error is EsErrorBody {
return error && error.error?.reason !== undefined;
}

export function isErrorString(error: any): error is string {
return typeof error === 'string';
}

export function isMLResponseError(error: any): error is MLResponseError {
return typeof error.body === 'object' && 'message' in error.body;
}

export function isBoomError(error: any): error is Boom {
return error.isBoom === true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import mockAnomalyRecord from './__mocks__/mock_anomaly_record.json';
import mockDetectorsByJob from './__mocks__/mock_detectors_by_job.json';
import mockJobConfig from './__mocks__/mock_job_config.json';

jest.mock('../../util/ml_error', () => class MLRequestFailure {});

jest.mock('../../services/job_service', () => ({
mlJobService: {
getJob() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import { ToastInput, ToastOptions, ToastsStart } from 'kibana/public';
import { useMemo } from 'react';
import { getToastNotifications } from '../../util/dependency_cache';
import { useNotifications } from '../../contexts/kibana';
import { MLRequestFailure } from '../../util/ml_error';
import { ErrorType, extractErrorProperties } from '../../../../common/util/errors';
import {
ErrorType,
extractErrorProperties,
MLRequestFailure,
} from '../../../../common/util/errors';

export type ToastNotificationService = ReturnType<typeof toastNotificationServiceProvider>;

Expand Down

0 comments on commit 430dbdb

Please sign in to comment.