You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: node.js/best-practices.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -387,7 +387,7 @@ Internally the [timestamp](events#timestamp) is a JavaScript `Date` object, that
387
387
388
388
When returning [Media Data](../guides/providing-services#serving-media-data) from a custom `READ`, `action`, or `function` handler, content information can be configured as part of the handlers result object.
389
389
390
-
Ideally, handlers use [`req.reply`](events#req-reply), calling it with an instance of [stream.Readable](https://nodejs.org/api/stream.html#class-streamreadable). Include options to specify content disposition headers:
390
+
Ideally, handlers use [`req.reply`](events#req-reply-results), calling it with an instance of [stream.Readable](https://nodejs.org/api/stream.html#class-streamreadable). Include options to specify content disposition headers:
@@ -241,7 +241,7 @@ Additional note about OData: For requests that are part of a changeset, the even
241
241
242
242
243
243
244
-
Class `cds.Request` extends [`cds.Event`] with additional features to represent and deal with synchronous requests to services in [event handlers](./core-services#srv-handle-event), such as the [query](#query), additional [request parameters](#params), the [authenticated user](#user), and [methods to send responses](#req-reply).
244
+
Class `cds.Request` extends [`cds.Event`] with additional features to represent and deal with synchronous requests to services in [event handlers](./core-services#srv-handle-event), such as the [query](#query), additional [request parameters](#params), the [authenticated user](#user), and [methods to send responses](#req-reply-results).
Rejects the request with the given HTTP response code and single message. Additionally, `req.reject` throws an error based on the passed arguments. Hence, no additional code and handlers is executed once `req.reject` has been invoked.
Use these methods to collect messages or errors and return them in the request response to the caller. The method variants reflect different severity levels. Use them as follows:
Soyoudon't have to (and should not) implement such checks manually in your code at all.
428
+
:::
399
429
400
-
{style="font-style:italic;width:80%;"}
401
430
402
-
**Note:** messages with a severity less than 4 are collected and accessible in property `req.messages`, while error messages are collected in property `req.errors`. The latter allows to easily check, whether errors occurred with:
-`code`_Number (Optional)_ - Represents the error code associated with the message. If the number is in the range of HTTP status codes and the error has a severity of 4, this argument sets the HTTP response status code.
412
-
-`message`_String \| Object \| Error_ - See below for details on the non-string version.
413
-
-`target`_String (Optional)_ - The name of an input field/element a message is related to.
414
-
-`args`_Array (Optional)_ - Array of placeholder values. See [Localized Messages](cds-i18n) for details.
415
485
416
-
::: tip `target` property for UI5 OData model
417
-
The `target` property is evaluated by the UI5 OData model and needs to be set according to [Server Messages in the OData V4 Model](https://ui5.sap.com/#/topic/fbe1cb5613cf4a40a841750bf813238e).
You can also pass an object as the sole argument, which then contains the properties `code`, `message`, `target`, and `args`. Additional properties are preserved until the error or message is sanitized for the client. In case of an error, the additional property `status` can be used to specify the HTTP status code of the response.
505
+
Aftereachphaseofrequestprocessing, i.e. _before_/_on_/_after_, theframeworkcheckswhethererrorsgotrecordedin`req.errors`. Ifso, itautomatically [rejects](#req-reject)therequestwithanaggregateerrorcontainingallrecordederrors, andtherequestisnotprocessedfurther. So, inessence,theaboveendsupintheequivalentof:
Additional properties can be added as well, for example to be used in [custom error handlers](core-services#srv-on-error).
435
517
436
-
> In OData responses, notifications get collected and put into HTTP response header `sap-messages` as a stringified array, while the others are collected in the respective response body properties (→ see [OData Error Responses](https://docs.oasis-open.org/odata/odata-json-format/v4.0/os/odata-json-format-v4.0-os.html#_Toc372793091)).
In production, errors should never disclose any internal information that could be used by malicious actors. Hence, we sanitize all server-side errors thrown by the CAP framework. That is, all errors with a 5xx status code (the default status code is 500) are returned to the client with only the respective generic message (example: `500 Internal Server Error`). Errors defined by app developers aren't sanitized and returned to the client unchanged.
Additionally, the OData protocol specifies which properties an error object may have. If a custom property shall reach the client, it must be prefixed with `@` to not be purged.
443
532
444
533
445
-
### req. diff() <Beta /> {.method}
446
-
[`req.diff`]: #req-diff
534
+
## ErrorResponses
447
535
448
-
Use this asynchronous method to calculate the difference between the data on the database and the passed data (defaults to `req.data`, if not passed). Note that the usage of `req.diff` only makes sense in *before* handlers as they are run before the actual change was persisted on the database.
Theerrorresponseisgeneratedfromtheerrorobjectconstructedvia [`req.reject()`](#req-reject) or [`req.error()`](#req-error), andthepropertiesareusedandnormalizedasfollows:
0 commit comments