Search

Application Service Providers

Class cds.ApplicationService is the default service provider implementation, adding generic handlers as introduced in the Cookbook guides on Providing Services, Localized Data and Temporal Data.

cds.ApplicationService class

class cds.ApplicationService extends cds.Service

Localized Messages / i18n

Generic Errors

You can provide localized error messages for a growing number of runtime errors. To do so, they simply need to provide messages_<locale>.properties files into one of the valid text bundles folders in the project root. See Where to Place Text Bundles? for more details.

Example:

// i18n/messages_en.properties
MULTIPLE_ERRORS=Multiple errors occurred.

[...]

// i18n/messages_de.properties
MULTIPLE_ERRORS=Es sind mehrere Fehler aufgetreten.

[...]

Custom Errors

You can define custom texts (incl. placeholders) and use them in the message API req.reject/error/info/warn(...). The respective text key is provided instead of the string message, and optional array of placeholder values are passed as the last parameter.

Example:

// i18n/messages_en.properties
ORDER_EXCEEDS_STOCK=The order of {0} books exceeds the stock by {1}

[...]

// srv/catalog-service.js
const cds = require('@sap/cds')

module.exports = (srv) => {
  const { Books, Orders } = srv.entities

  srv.before('CREATE', Orders, async (req) => {
    const book = await SELECT.one(Books).where({ ID: req.data.book_ID })
    if (book.stock < req.data.amount) {
      req.reject(400, 'ORDER_EXCEEDS_STOCK', [req.data.amount, req.data.amount - book.stock])
    }
  })
}

List of Generic Texts

Please find the current list of generic runtime texts:

400=Bad Request
401=Unauthorized
403=Forbidden
404=Not Found
405=Method Not Allowed
406=Not Acceptable
407=Proxy Authentication Required
408=Request Timeout
409=Conflict
410=Gone
411=Length Required
412=Precondition Failed
413=Payload Too Large
414=URI Too Long
415=Unsupported Media Type
416=Range Not Satisfiable
417=Expectation Failed
424=Failed Dependency
428=Precondition Required
429=Too Many Requests
431=Request Header Fields Too Large
451=Unavailable For Legal Reasons
500=Internal Server Error
501=The server does not support the functionality required to 
fulfill the request
502=Bad Gateway
503=Service Unavailable
504=Gateway Timeout

MULTIPLE_ERRORS=Multiple errors occurred. Please see the details 
for more information.

More to Come…

This documentation is not complete yet, or the APIs are not released for general availability. There’s more to come in this place in upcomming releases…