cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to throw errors from REST API calls (CAP CDS and Fiori app)

Harshi
Product and Topic Expert
Product and Topic Expert
0 Likes
778

I have a CAP and Fiori app. For a particular value help field, I call a REST API inside the handler on READ event of the field. In case the API responds in errors, I can see those errors only in the network tab. The ODATA API call still shows 200. How to propagate the exception to UI?

Accepted Solutions (0)

Answers (1)

Answers (1)

T1mey
Active Participant
0 Likes

Have you tried

req.error(40x, "error msg");

https://cap.cloud.sap/docs/node.js/events#req-error

Harshi
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi T1mey,
Thank you for your reply. I use Kotlin for Handlers and I use the req.err equivalent which is ServiceException. I use it as below

catch (e: RestClientResponseException) {
            log.error { "Failed. Response: ${e.statusCode} ${e.responseBodyAsString}" }
            if (e.statusCode == NOT_FOUND) {
                messages.errorCode("notFound")
            }
            messages.errorCode("error", e.responseBodyAsString)
            messages.throwIfError()
        }

 

Harshi
Product and Topic Expert
Product and Topic Expert
0 Likes
Hi T1mey, This does not help either