Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RESTful API中的Typescript自定义错误_Javascript_Node.js_Typescript_Rest_Error Handling - Fatal编程技术网

Javascript RESTful API中的Typescript自定义错误

Javascript RESTful API中的Typescript自定义错误,javascript,node.js,typescript,rest,error-handling,Javascript,Node.js,Typescript,Rest,Error Handling,我有一个从另一个项目截取的代码,在RESTful API中有自定义错误。在我将其重构为typescript之前,这一切都很好。我不理解错误构造函数是如何工作的,这个范围内不知道这个.response 我如何抛出此错误 async function authenticate(request, response, next) { if(!request.body.email) { return next(new ErrorREST(Errors.BadRequest, "Us

我有一个从另一个项目截取的代码,在RESTful API中有自定义错误。在我将其重构为typescript之前,这一切都很好。我不理解错误构造函数是如何工作的,这个范围内不知道这个.response

我如何抛出此错误

async function authenticate(request, response, next) {
    if(!request.body.email) {
        return next(new ErrorREST(Errors.BadRequest, "User name missing."));
    }
}
return next(new ErrorREST(Errors.Unauthorized));
return next(new ErrorREST(Errors.Unauthorized), "Authentication credentials not valid.");
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."
  }
}

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};
   }
}
 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);
const错误={
请求:{
现状:400,
消息:“请求的格式错误。”
},
未经授权:{
状况:401,
消息:“身份验证凭据无效。”
},
禁止:{
现状:403,
消息:“您缺少执行此请求的权限。”
}
}
类ErrorREST扩展了Error{
构造函数(类型,细节=未定义,…参数){
超级(…args);
如果(类型的类型!=='object'){
返回新错误(“您需要提供错误类型”);
}
这个。响应=类型;
如果(细节!==未定义){
this.response.detail=细节;
}
}

}
JavaScript在调用时创建此.response。所以我创建了这个字段,TypeScript就知道了

第二个问题是,在错误处理之后,我在app.ts中定义了我的路由

错误。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};
   }
}
 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);
应用程序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};
   }
}
 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);
将错误返回给用户

async function authenticate(request, response, next) {
    if(!request.body.email) {
        return next(new ErrorREST(Errors.BadRequest, "User name missing."));
    }
}
return next(new ErrorREST(Errors.Unauthorized));
return next(new ErrorREST(Errors.Unauthorized), "Authentication credentials not valid.");

这里的问题并不明显。
type
参数的类型必须为
{status:number,message:string}
。如果不是这样,你的问题是什么?这是一个很好的提示,但主要问题是这个。在这个范围内不知道响应。因为我不知道这在JavaScript中是如何工作的,所以我无法在typescript中修复它