I have a code sniped from another project with custom errors in a RESTful API. This all worked fine until i refactored it to typescript. I did not understand how the error construktor works and this.response is not know in this scope.
How i throw this error
async function authenticate(request, response, next) {
if(!request.body.email) {
return next(new ErrorREST(Errors.BadRequest, "User name missing."));
}
}
error.js
const Errors = {
BadRequest: {
status: 400,
message: "Request has wrong format."
},
Unauthorized: {
status: 401,
message: "Authentication credentials not valid."
},
Forbidden: {
status: 403,
message: "You're missing permission to execute this request."
}
}
class ErrorREST extends Error {
constructor(type, detail = undefined, ...args) {
super(...args);
if (typeof type !== 'object') {
return new Error("You need to provide the error type.");
}
this.response = type;
if (detail !== undefined) {
this.response.detail = detail;
}
}
}
I have not found a similar solution. This solution provides predefined errors with additional custom messages.

typeparameter needs have a type of{ status: number, message: string}. If not that, what is your question?