Skip to content

Commit

Permalink
feat(rest): Improve OnmsError object and GrafanaHttp error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus von Rüden committed Aug 22, 2017
1 parent 0ea573f commit 72d6e32
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
39 changes: 14 additions & 25 deletions src/api/OnmsError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,39 @@
* Represents an OpenNMS.js error. This will eventually have custom stuff to do... stuff.
* @module OnmsError
*/
export class OnmsError {
export class OnmsError extends Error {
/**
* The response status code, if any.
* @hidden
*/
private statusCode: number;

/**
* We need a real JS Error because it can't be subclassed.
* @hidden
*/
private errorObj: Error;
private data: any;

/**
* The stack trace so this object is loggable.
* @hidden
*/
private stack;
private options: any;

/** The error code associated with this error. */
public get code() {
return this.statusCode;
}

/** The JS Error class associated with this error. */
public get error() {
return this.errorObj;
}

/** The error message. */
public get message() {
return this.errorObj.message;
}

/**
* Create a new error.
* @constructor
* @param message - The error message.
* @param code - An optional error code to associate with the error.
*/
constructor(public mess: string, code?: number) {
this.errorObj = new Error(mess);
this.statusCode = code;
this.stack = this.errorObj.stack;
constructor(message: string, code?: number, options?: any, data?: any) {
super(message);
this.name = this.constructor.name;
this.statusCode = code;
this.data = data;
this.options = options;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = (new Error(message)).stack;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/rest/AbstractHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export abstract class AbstractHTTP implements IOnmsHTTP {
* A callback to handle any request errors.
* @hidden
*/
protected handleError(err: any): never {
protected handleError(err: any, options?: any): never {
const message = AbstractHTTP.extractMessage(err);
const status = AbstractHTTP.extractStatus(err);
if (status) {
Expand Down
12 changes: 12 additions & 0 deletions src/rest/GrafanaError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {OnmsError} from '../api/OnmsError';

export class GrafanaError extends OnmsError {

private config: any;

constructor(message: string, code?: number, options?: any, data?: any) {
super(message, code, options, data);
this.config = options;
}

}
31 changes: 26 additions & 5 deletions src/rest/GrafanaHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {log, catRest} from '../api/Log';
import {Category} from 'typescript-logging';

import * as clonedeep from 'lodash.clonedeep';
import {Util} from '../internal/Util';
import {GrafanaError} from './GrafanaError';

/** @hidden */
const catGrafana = new Category('grafana', catRest);
Expand Down Expand Up @@ -52,7 +52,9 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}).catch((e) => {
this.handleError(e, query);
});
}

/** Make an HTTP PUT call using the Grafana `BackendSrv`. */
Expand All @@ -72,7 +74,9 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}).catch((e) => {
this.handleError(e, query);
});
}

/** Make an HTTP POST call using the Grafana `BackendSrv`. */
Expand All @@ -91,7 +95,9 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}).catch((e) => {
this.handleError(e, query);
});
}

/** Make an HTTP DELETE call using the Grafana `BackendSrv`. */
Expand All @@ -110,9 +116,24 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}).catch((e) => {
this.handleError(e, query);
});
}

/**
* A callback to handle any request errors.
* @hidden
*/
protected handleError(err: any, options?: any): never {
let message = AbstractHTTP.extractMessage(err);
const status = AbstractHTTP.extractStatus(err);
if (!status) {
message = 'An unknown error has occurred: ' + message;
}
throw new GrafanaError(message, status, options, err);
}

/**
* Internal method to turn [[OnmsHTTPOptions]] into a Grafana `BackendSrv` request object.
* @hidden
Expand Down

0 comments on commit 72d6e32

Please sign in to comment.