Permalink
Browse files

feat(rest): Improve OnmsError object and GrafanaHttp error handling

  • Loading branch information...
mvrueden committed Aug 22, 2017
1 parent 0ea573f commit 72d6e32153e2518b61b407e79ab056d901c4a364
Showing with 53 additions and 31 deletions.
  1. +14 −25 src/api/OnmsError.ts
  2. +1 −1 src/rest/AbstractHTTP.ts
  3. +12 −0 src/rest/GrafanaError.ts
  4. +26 −5 src/rest/GrafanaHTTP.ts
View
@@ -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;
}
}
/**
View
@@ -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) {
View
@@ -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;
}
}
View
@@ -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);
@@ -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`. */
@@ -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`. */
@@ -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`. */
@@ -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

0 comments on commit 72d6e32

Please sign in to comment.