我目前试图打印一个嵌套的错误与Javascript,但我似乎只得到内部消息作为字符串:
axios.post('http://localhost:8080/axios, data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error.response.data.message);
});
状态500读取Api#创建(字符串、字符串、请求);内容:{“timestamp”:“2018-10-30T12:08:40.524 0000”,“status”:500,“error”:“Internal Server error”,“message”:“EntityStateError[message=这是我要打印的实际错误,code=400,service=service,embeddedErrors=]\r\n”,“path”:“/axios”}
我只想在消息之后打印错误(“这是我的实际错误…”)。
我想我可以将它解析为JSON,但是当我使用
console.log(JSON.parse( '"' + error.response.data.message + '"'));
我得到以下错误:
未捕获(promise中)SyntaxError:JSON中位于JSON位置97处的意外标记。解析()
如何访问错误响应中的实际消息?
error.response.data本身:
{时间戳:“2018-10-30T13:31:09.097 0000”,状态:500,错误:“内部服务器错误”,消息:“状态500读取Api#create/axios”},路径:“/axios”}错误:“内部服务器错误”消息:“状态500读取Api#create(字符串、字符串、请求);内容:↵{“timestamp”:“2018-10-30T13:31:09.076 0000”,“status”:500,“error”:“Internal Server error”,“message”:“EntityStateError[message=这是我要打印的实际错误,code=400,service=Cancellation,embeddedErrors=]\r\n”,“path”:“/axios”}”路径:“/axios”状态:500时间戳:“2018-10-30T13:31:09.097 0000”
我现在找到了一个解决方案,将收到的字符串拆分,直到只剩下message和code之间的部分(返回我要打印的错误消息)。
let innerError = error.response.data.message;
innerError = innerError.split('content:\n')[1];
innerError = innerError.split('message=').pop().split(',code=')[0];
console.log(innerError);