最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript custom errors in RESTful API - Stack Overflow

programmeradmin5浏览0评论

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 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.

Share Improve this question edited Mar 3, 2019 at 19:39 geisterfurz007 5,8946 gold badges35 silver badges58 bronze badges asked Feb 26, 2019 at 18:58 Janneck LangeJanneck Lange 9324 gold badges14 silver badges34 bronze badges 2
  • 1 It's not obvious what the issue is here. The type parameter needs have a type of { status: number, message: string}. If not that, what is your question? – zeh Commented Feb 26, 2019 at 23:36
  • This is a good hint, but the main problem was this.response is not known in this scope. And because I don't know how this works in JavaScript i cant fix it in typescript – Janneck Lange Commented Feb 28, 2019 at 12:37
Add a ment  | 

1 Answer 1

Reset to default 3

JavaScript create this.response at the moment you call it. So i create this field and typecript knew it.

Second problem was, i defined my routes in the app.ts after my error handling.

error.ts

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."
  }
}

export class ErrorREST extends Error {
   public response: { status: number; message: string; detail: string };

    constructor(error: { status: number, message: string }, detail: string = undefined, ...args) {
       super(...args);
       this.response = {status: error.status, message: error.message, detail: detail};
   }
}

app.ts

 this.express.use('/api/users', usersRouter);

 this.express.use(function (error, request, response, next) {

      logRequest(console.error, request);
      console.error("ERROR OCCURRED:");
      console.error(error);

   if (error == null || error.response == null) {//error is not a custom error
      error = new ErrorREST(Errors.InternalServerError);
   } 

   response.status(error.response.status).send(error.response);

return your error to user

return next(new ErrorREST(Errors.Unauthorized));
return next(new ErrorREST(Errors.Unauthorized), "Authentication credentials not valid.");

发布评论

评论列表(0)

  1. 暂无评论