taapi-cache
Version:
TAAPI.IO Cache Package. A convenient way to fetch candles, store them and reuse them.
59 lines (53 loc) • 1.39 kB
JavaScript
/**
* Handles error throwing, the constructer needs the context object which
* should hold a property 'response'. This response object is set by the
* AuthVerification middleware.
*/
class Errors {
constructor(ctx) {
this.ctx = ctx;
}
/**
* Throws an actual error of a certain ErrorTypes.
*
* @param {*} errorType One of the ErrorTypes defined by this package. Example: UNAUTHORIZED
*/
throwError(errorType) {
// Set the status code in the response headers
this.ctx.response.statusCode = errorType.statusCode; // Throw the error
throw new Error(errorType.message);
}
}
/**
* Define error types that may be returned by this API
*/
const errorTypes = {
OK: {
message: "200: Request OK!",
statusCode: 200
},
BAD_REQUEST: {
message: "400: Bad request. Check your request parameters and try again!",
statusCode: 400
},
UNAUTHORIZED: {
message: "401: You are not authenticated to query this endpoint!",
statusCode: 401
},
FORBIDDEN: {
message: "403: Permission denied for this endpoint!",
statusCode: 403
},
NOT_FOUND: {
message: "404: The endpoint you are calling does not exist!",
statusCode: 404
},
INTERNAL_SERVER_ERROR: {
message: "500: An internal server error has occurred!",
statusCode: 500
}
};
module.exports = {
Errors: Errors,
ErrorTypes: errorTypes
};