Permalink
Browse files

feat(rest): return a useful OnmsResult on error

  • Loading branch information...
RangerRick committed Jul 25, 2017
1 parent d3af9e2 commit 094399c22a77a04b94bba464359bc184bb6d1de8
Showing with 58 additions and 6 deletions.
  1. +52 −0 src/rest/AbstractHTTP.ts
  2. +3 −3 src/rest/AxiosHTTP.ts
  3. +3 −3 src/rest/GrafanaHTTP.ts
View
@@ -143,4 +143,56 @@ export abstract class AbstractHTTP implements IOnmsHTTP {
// do nothing by default
}
/**
* A callback to handle any request errors.
* @hidden
*/
protected handleError(err: any): never {
const message = AbstractHTTP.extractMessage(err);
const status = AbstractHTTP.extractStatus(err);
if (status) {
throw OnmsResult.error(message, status);
} else {
throw OnmsResult.error('An unknown error has occurred: ' + message);
}
}
/* tslint:disable:member-ordering */
/**
* Attempt to determine an error message from an error response.
* @hidden
*/
protected static extractMessage(err: any): string {
if (err) {
if (err.message) {
return err.message;
} else if (err.response) {
return this.extractMessage(err.response);
} else if (err.data && Object.prototype.toString.call(err) === '[object String]') {
return err.data;
} else if (err.statusText) {
return err.statusText;
}
return JSON.stringify(err);
}
return 'no error message';
}
/**
* Attempt to determine an error status code from an error response.
* @hidden
*/
protected static extractStatus(err: any): number {
let status;
if (err.code) {
status = err.code;
} else if (err.status) {
status = err.status;
} else if (err.response && err.response.status) {
status = err.response.status;
}
return status;
}
}
View
@@ -70,7 +70,7 @@ export class AxiosHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}).catch(this.handleError);
}
/**
@@ -94,7 +94,7 @@ export class AxiosHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}).catch(this.handleError);
}
/**
@@ -117,7 +117,7 @@ export class AxiosHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}).catch(this.handleError);
}
/**
View
@@ -51,7 +51,7 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}).catch(this.handleError);
}
/** Make an HTTP PUT call using the Grafana `BackendSrv`. */
@@ -71,7 +71,7 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}).catch(this.handleError);
}
/** Make an HTTP POST call using the Grafana `BackendSrv`. */
@@ -90,7 +90,7 @@ export class GrafanaHTTP extends AbstractHTTP {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}).catch(this.handleError);
}
/**

0 comments on commit 094399c

Please sign in to comment.