简体   繁体   English

访问 Typescript-ES5 或节点错误消息

[英]Accessing Typescript-ES5 or Node Error message

I have an error handler.我有一个错误处理程序。 I don't know if it is Node or Typescript-ES5 Error object.不知道是Node还是Typescript-ES5错误object。 When I inspect in the console, I have the following当我在控制台中检查时,我有以下内容

{…}
error: Object { message: "message that I want to get" }
headers: Object { normalizedNames: Map(0), lazyUpdate: null, lazyInit: lazyInit()
 }
message: "Http failure response for http://localhost:4200/someEnpointUrl: 400 Bad Request"
name: "HttpErrorResponse"
ok: false
status: 400
statusText: "Bad Request"
url: "http://localhost:4200/someEnpointUrl"
<prototype>: Object { … }

Inspecting the Error object the only information I can get is error.name and error.message, but in my case I get error.message = "Http failure response for http://localhost:4200/someEnpointUrl: 400 Bad Request" and error.name = HttpErrorResponse检查错误 object 我能得到的唯一信息是 error.name 和 error.message,但在我的情况下,我得到 error.message = "Http failure response for http://localhost:4200/someEnpointUrl: 400 Bad Request" 和错误.name = HttpErrorResponse

On the back-end, the error is coming from在后端,错误来自

throw new ControllerException(HttpStatus.BAD_REQUEST, "message that I want to get"); throw new ControllerException(HttpStatus.BAD_REQUEST, "我想得到的消息");

How can I get the error message on the front end (error: Object { message: "message that I want to get" }) The editor cannot get this object property?前端怎么获取报错信息(error: Object { message: "message that I want to get" })编辑器获取不到这个object属性?

I found the solution: I created a custom error我找到了解决方案:我创建了一个自定义错误

export class ExtendedError extends Error{

  public error!: { message: string }
  constructor(m: string) {
    super(m);
  }
}

and can get error.error.message The discussion is closed并且可以得到 error.error.message 讨论结束

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM