簡體   English   中英

如何打印從 REST 返回的嵌套錯誤消息?

[英]How can I print a nested error message returned from REST?

我目前正在嘗試使用 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#create(String,String,Request); 內容:{"timestamp":"2018-10-30T12:08:40.524+0000","status":500,"error":"內部服務器錯誤","message":"EntityStateError[message=這是我的實際我想打印的錯誤。,code=400,service=Service,embeddedErrors=]\\r\\n","path":"/axios"}

我只想在消息之后打印錯誤(“這是我的實際錯誤......”)。

我以為我可以將其解析為 JSON,但是當我使用

console.log(JSON.parse( '"' + error.response.data.message + '"'));

我收到以下錯誤:

Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 97 at JSON.parse ()

如何訪問錯誤響應中的實際消息?

error.response.data 本身:

{timestamp: "2018-10-30T13:31:09.097+0000", status: 500, error: "Internal Server Error", message: "status 500 reading Api#create/axios"}", path: "/axios"錯誤:“內部服務器錯誤”消息:“狀態 500 讀取 Api#create(String,String,Request); content:↵{"timestamp":"2018-10-30T13:31:09.076+0000","status":500,"error":"內部服務器錯誤","message":"EntityStateError[message=This is my我想打印的實際錯誤。,code=400,service=Cancellation,embeddedErrors=]\\r\\n","path":"/axios"}" path: "/axios" status: 500 timestamp: "2018 -10-30T13:31:09.097+0000"

現在,我找到了一種解決方案,方法是拆分收到的字符串,直到僅剩下消息和代碼之間的部分為止(這將返回我要打印的錯誤消息)。

 let innerError = error.response.data.message; innerError = innerError.split('content:\\n')[1]; innerError = innerError.split('message=').pop().split(',code=')[0]; console.log(innerError); 

但是,這並不是一個干凈的解決方案,因此,如果有人願意提供幫助,我仍在尋找輸入。

.catch(err){
   throw err.message;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM