简体   繁体   English

我想在响应正文中使用 Throw function 抛出带有状态代码的自定义错误

[英]I want to throw custom Error with status code using Throw function in response body

If I use Throw new Error("User not found"), then it gives in response如果我使用 Throw new Error("User not found"),那么它会给出响应

{status:false,message:"User Not Found"}

But with status code 500, And I need Status 400 in Postman但是状态码为 500,我需要 Postman 中的状态码 400

custom Error using throw function自定义错误使用 throw function

but if we use res.status(500).send({ status: false, message: "User not found" })但是如果我们使用res.status(500).send({ status: false, message: "User not found" })

then it gives status code 400, And I need Status 400 in Postman. So, I need same status code in postman only.This is the problem.然后它给出状态代码 400,我需要 Postman 中的状态 400。因此,我只需要 postman 中的相同状态代码。这就是问题所在。 Tyler2P and Abin Bala, I followed your code but I am unable to get desired status code in postman status. Tyler2P 和 Abin Bala,我遵循了你的代码,但我无法在 postman 状态中获得所需的状态代码。

You can create a custom error class as below and throw it with appropriate message and httpCode.您可以创建一个自定义错误 class,如下所示,并使用适当的消息和 httpCode 将其抛出。 You can also add more properties.您还可以添加更多属性。 Then you can catch the custom error object using the catch block and get the required values.然后您可以使用 catch 块捕获自定义错误 object 并获取所需的值。

class CustomError extends Error {
  name;
  httpCode;
  message;
  
  
  constructor(name,httpCode, message){
    super(message);
    this.name = name;
    this.httpCode = httpCode;
    this.message = message;
  }
}

errorThrowingFunction.js : errorThrowingFunction.js

//import the custom error class in the module that you //are   going to use it.
errorThrowingFunction = () => {
  const authToken = myCache.get("token");
      
  if (!authToken) {
    throw new CustomError('Error',401,'token missing');
  } else {
    return authToken;
  }
}

index.js : index.js

handler = () => {
  try {
    errorThrowingFunction();
  } catch(error){
    const response = {
      statusCode: error.httpCode,
      body: JSON.Stringify(error.message),
      isBase64Encoded: false,
      //add other headers
    }
    return response;
    //if you are using this in rest service, the use below line
    //return res.status(error.httpCode)send(response);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 fetch 调用中抛出带有响应正文的自定义错误 - Throw a custom error with response body in fetch call 如何通过then()将Fetch响应的JSON主体传递给Throw Error()? - How can I pass JSON body of Fetch response to Throw Error() with then()? 我可以根据响应状态在 axios 帖子中抛出错误吗 - Can I throw error in axios post based on response status 如何在Ajax(Post)请求期间抛出自定义http状态代码 - How throw custom http status code during Ajax(Post) request 为什么函数体中的冒号不会在JavaScript中抛出错误? - Why doesn’t a colon in function body not throw an error in JavaScript? 节点服务器响应错误:process.nextTick(function(){throw err;}); - Node server response error: process.nextTick(function(){throw err;}); RxJS - 为什么我要使用 throwError 而不是简单地抛出错误? - RxJS - Why would I want to use throwError and not simply throw an error? 如何使用javascript在计算器中抛出错误? 自定义错误不是默认错误?使用特殊处理 - How will I throw error in calculator using javascript? custom error not default errors?using exceptional handling 实际上我想运行每个循环和 append 它,但是在 append 里面我想使用模板文字运行另一个循环但是它抛出错误 - Actually I want to run each loop and append it, but inside append I want to run another loop using template literals but it throw error 使用 Jest 测试应该抛出错误的 function,但总是收到“函数未抛出”错误 - Using Jest to test a function which should throw an error, but always received “function did not throw” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM