cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CAP - Access Request Body in error handler for 400 - Bad Request error

lorenzobigs
Discoverer

Hi experts,

in a classic SAP CAP application, Node.js flavour, i have defined a service.js implementation for my service.cds , with a generic error handler:

service.js

const cds = require('@sap/cds'); 

module.exports = async function () {
  /**
  * The generic error handler
  */
  this.on('error', (err, req) => {
    //How to retrieve req.data?
    console.log('Oh no! ' + err.message);
  });
};

Let's say that I execute a POST to an entity that throws a DeserializationError. The error is being catched by the error handler but the req.data is undefined.

Is there a way in this scenario to access to the body of the request which caused the error?

Accepted Solutions (1)

Accepted Solutions (1)

Arley
Product and Topic Expert
Product and Topic Expert

In this context, using context propagation works:

 

cds.context.http.req.data

 

lorenzobigs
Discoverer
0 Kudos

Hi @Arley , thank you, this works fine for entity served as projection of the ones in the data-model.cds.

In the scenario in which I am exposing an action with a custom defined type, the cds.context.http.req.data it is still undefined. Any suggestions?

 

define type EventMessage {
        ID       :      UUID;
        time     :      DateTime;
        name     :      String;
        category :      String;
        status   :      String;

    }
... 

action add @(restrict: [
        {
            grant: ['WRITE'],
            to   : 'admin'
        }])(
            ID   : EventMessage:ID,
            time : EventMessage:time,
            name : EventMessage:name,
            category : EventMessage:category,
            status : EventMessage:status
            ) returns String;

 

 

 

Answers (0)