Catching Backend errors
See original GitHub issueIs your feature request related to a problem? Please describe. Not being able to get hands on the error response provided by backend
Describe the solution you’d like Basically, I go along these lines
myExtOdataV2service.entityset.reauestbuilder(). ... .execute(destination)
and the request is sent correctly to the source system, which returns a (perfectly valid) error response as odata v2 json, which I need to grab and pass along.
Adding a catch()
at the end of the above statement, I get an error object, containing the original json somewhere in the middle of the stacktrace string…
Sure, I could go and try to find it using regex or something, but is there a “proper” way to catch the original backend error in @sap-cloud-sdk?
Describe alternatives you’ve considered Sure, I could go and try to find it using regex in the stacktrace.
Additional context example stacktrace inside the catch
Create request failed!
at Object.errorWithCause (XXX/srv/node_modules/@sap-cloud-sdk/util/dist/error.js:5:20)
at XXX/srv/node_modules/@sap-cloud-sdk/core/dist/odata/v2/request-builder/create-request-builder.js:124:54
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at ...
Caused by:
Error: post request to https://*****_SRV failed! Personnel Number ******** : Incorrect data in ***
{"code":"***/***","message":{"lang":"en","value":"Personnel Number ******** : Incorrect data in ***"},"innererror":{"application":{"component_id":"","service_namespace":"/***/","service_id":"***","service_version":"0001"},"transactionid":"***","timestamp":"20201027150920.7447650","Error_Resolution":{"SAP_Transaction":"For backend administrators: run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details","SAP_Note":"See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"},"errordetails":[{"code":"***/***","message":"Personnel Number ******** : Incorrect data in ***","longtext_url":"/sap/opu/odata/iwbep/message_text;o=LOCAL/","propertyref":"","severity":"error","target":""}]}}
at Object.errorWithCause (XXX/srv/node_modules/@sap-cloud-sdk/util/dist/error.js:5:20)
at constructError (XXX/srv/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:295:23)
at XXX/srv/node_modules/@sap-cloud-sdk/core/dist/odata/common/request/odata-request.js:265:51
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at ...
Caused by:
Error: Request failed with status code 400
at createError (XXX/srv/node_modules/axios/lib/core/createError.js:16:15)
at settle (XXX/srv/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (XXX/srv/node_modules/axios/lib/adapters/http.js:236:11)
at IncomingMessage.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1223:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Feature implementation
sorry for not providing a pull request, here is a quick solution:
@sap-cloud-sdk/util/dist/error.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorWithCause = void 0;
function errorWithCause(message, cause) {
var newError = new Error(message);
// Stack is a non-standard property according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types
if (newError.stack && cause.stack) {
newError.stack = newError.stack + '\nCaused by:\n' + cause.stack;
}
// INSERT BEGIN -- @the-docta
if (cause.isAxiosError) {
// these are the fields set by axios in lib/core/enhanceError (as of 0.19.2)
newError.isAxiosError = cause.isAxiosError
newError.config = cause.config;
newError.request = cause.request;
newError.response = cause.response;
if (cause.code) {
newError.code = cause.code;
}
}
// INSERT END -- @the-docta
return newError;
}
exports.errorWithCause = errorWithCause;
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (7 by maintainers)
Hey @the-docta,
you telling us, that it is high priority for you is already making it higher priority, but it was never really low priority, it is just not being worked at yet (we’re quite a small team). I changed the name in the board to backlog to avoid future confusion. 😉 We are planning on discussing possible solutions next week. Once we have decided on an approach I will link the the ADR for this here, it would be great to hear your feedback on it then.
After that it can be implemented. This can be either done by us, or if you are willing to contribute of course you are also welcome to do so. In any case, I’ll keep you posted.
Released in version 1.34.0. Feel free to open a new issue in case you have further requirements.