2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Thrown when there is a problem with a given API call. This is special-cased in the responder
|
|
|
|
* to create a JSON error response with the status code given.
|
|
|
|
*/
|
2017-12-18 02:22:09 +00:00
|
|
|
export class ApiError {
|
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* The HTTP status code to return
|
|
|
|
*/
|
2017-12-18 02:22:09 +00:00
|
|
|
public statusCode: number;
|
2018-03-30 21:17:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An object to be returned as JSON to the caller
|
|
|
|
*/
|
|
|
|
public jsonResponse: object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal error code to describe what went wrong
|
|
|
|
*/
|
2018-03-24 03:01:56 +00:00
|
|
|
public errorCode: string;
|
2017-12-18 02:22:09 +00:00
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Creates a new API error
|
|
|
|
* @param {number} statusCode The HTTP status code to return
|
|
|
|
* @param {string|object} json An object to be returned as JSON or a message to be returned (which is
|
|
|
|
* then converted to JSON as {message: "your_message"})
|
|
|
|
* @param {string} errCode The internal error code to describe what went wrong
|
|
|
|
*/
|
|
|
|
constructor(statusCode: number, json: string | object, errCode = "D_UNKNOWN") {
|
2017-12-18 02:22:09 +00:00
|
|
|
// Because typescript is just plain dumb
|
|
|
|
// https://stackoverflow.com/questions/31626231/custom-error-class-in-typescript
|
|
|
|
Error.apply(this, ["ApiError"]);
|
|
|
|
|
2017-12-24 09:02:57 +00:00
|
|
|
if (typeof(json) === "string") json = {message: json};
|
2017-12-18 02:22:09 +00:00
|
|
|
this.jsonResponse = json;
|
|
|
|
this.statusCode = statusCode;
|
2018-03-24 03:01:56 +00:00
|
|
|
this.errorCode = errCode;
|
2017-12-18 02:22:09 +00:00
|
|
|
}
|
|
|
|
}
|