cancel
Showing results for 
Search instead for 
Did you mean: 

How to throw multiple errors with a function called by CAP handler?

epamtiosteel
Explorer
0 Kudos

Hi colleagues,

usually if multiple errors are expected we throw it like this:

srv.on('CREATE', 'myEntity' req => {
if (true) { req.error(400, 'nope 1'); req.error(400, 'nope 2'); return; }
})

Sometimes though we want to clean up our handler so that it turns into kind of this:

validateBeforeCreateMyEntity(req) {
   if (true) {
     req.error(400, 'nope 1');
     req.error(400, 'nope 2');

     // here I want to reject the request with multiple errors which means I probably can't use req.reject as it returns only a single error
   }
}

srv.before('CREATE', 'myEntity' req => {
validateBeforeCreateMyEntity(req); // assume we have 50 more lines here
})

Is there currently any way to do it?

p.s currently in CAP documentation it's just

if (req.errors) //> get out somehow...

Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

vitaly_kozyura
Participant

HI Mikhail,

An example rejecting with multiple errors in before handler is here: https://cap.cloud.sap/docs/node.js/core-services#srv-handle-event

// before phase

await Promise.all (matching .before handlers)

if (req.errors) throw req.reject() // without args

Regards,

Vitaly

Answers (1)

Answers (1)

epamtiosteel
Explorer
0 Kudos

for history:

req.reject(); // without parameters

works like a charm.

vitaly.kozyura thanks a lot!