Class RESTResponseMgr
This class provides helper methods for creating REST error and success responses. It is mainly intended to be used to
build Custom REST APIs. But, any controller implementation planning to provide REST-like responses can use these
methods. If these methods are being used in the controllers, note that a few defaults like URL prefix for
type in createError methods will correspond to Custom REST APIs.
| Constructor | Description |
|---|---|
| RESTResponseMgr() |
| Method | Description |
|---|---|
| static createEmptySuccess(Number) | Constructs a new RESTSuccessResponse object. |
| static createError(Number) | Constructs a new RESTErrorResponse object. |
| static createError(Number, String) | Constructs a new RESTErrorResponse object. |
| static createError(Number, String, String) | Constructs a new RESTErrorResponse object. |
| static createError(Number, String, String, String) | Constructs a new RESTErrorResponse object. |
| static createScapiRemoteInclude(String, String, String, String, URLParameter...) | Constructs a new RemoteInclude object specific for the SCAPI include path. |
| static createStorefrontControllerRemoteInclude(URLAction, URLParameter...) | Constructs a new RemoteInclude object specific for the Storefront Controller include path. |
| static createSuccess(Object) | Constructs a new RESTSuccessResponse object. |
| static createSuccess(Object, Number) | Constructs a new RESTSuccessResponse object. |
assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values
- RESTResponseMgr()
- static createEmptySuccess(statusCode: Number): RESTSuccessResponse
Constructs a new RESTSuccessResponse object. This method is to be used in scenarios where response body is not expected (e.g. statusCode is 204).
Parameters:
- statusCode - The http status code of the response. The statusCode parameter should conform to RFC standards for a success.
Returns:
- A new RESTSuccessResponse object.
Throws:
- IllegalArgumentException - If the statusCode is not in the (100..299) range.
- static createError(statusCode: Number): RESTErrorResponse
Constructs a new RESTErrorResponse object. This method should be used when you have just the statusCode of the error and want the type of error to be inferred.
'type' of the error is inferred from the status code as follows:
400-bad-request401-unauthorized403-forbidden404-resource-not-found409-conflict412-precondition-failed429-too-many-requests500-internal-server-errordefault-about:blank
Parameters:
- statusCode - The error code of the response. The statusCode parameter should conform to RFC standards for an error.
Returns:
- A new RESTErrorResponse object.
Throws:
- IllegalArgumentException - If the statusCode is not in the (400..599) range.
- static createError(statusCode: Number, type: String): RESTErrorResponse
Constructs a new RESTErrorResponse object. This method should be used when you want to omit 'title' and 'detail' of the error. With this method, custom error codes and types apart from the standard ones can be constructed.
Parameters:
- statusCode - The error code of the response. The statusCode parameter should conform to RFC standards for an error.
- type - Type of the error according to RFC 9457. We enforce the following restrictions on top of the RFC: <li>If the provided type is not an absolute URL, it will be prepended with
https://api.commercecloud.salesforce.com/documentation/error/v1/custom-errors/.</li> <li>Custom error types are not allowed to have SYSTEM error type prefix:https://api.commercecloud.salesforce.com/documentation/error/v1/errors/.</li> </ul>
Returns:
- A new RESTErrorResponse object.
Throws:
- IllegalArgumentException - If the statusCode is not in the (400..599) range or if the error type is not a valid URI or conflicts with the SYSTEM error type namespace.
- static createError(statusCode: Number, type: String, title: String): RESTErrorResponse
Constructs a new RESTErrorResponse object. This method should be used when you want to omit 'detail' of the error but want to have valid 'statusCode', 'type' and 'title'.
Parameters:
- statusCode - The error code of the response. The statusCode parameter should conform to RFC standards for an error.
- type - Type of the error according to RFC 9457. We enforce the following restrictions on top of the RFC: <li>If the provided type is not an absolute URL, it will be prepended with
https://api.commercecloud.salesforce.com/documentation/error/v1/custom-errors/.</li> <li>Custom error types are not allowed to have SYSTEM error type prefix:https://api.commercecloud.salesforce.com/documentation/error/v1/errors/.</li> </ul> - title - Human-readable summary of the error type.
Returns:
- A new RESTErrorResponse object.
Throws:
- IllegalArgumentException - If the statusCode is not in the (400..599) range or if the error type is not a valid URI or conflicts with SYSTEM error type namespace.
- static createError(statusCode: Number, type: String, title: String, detail: String): RESTErrorResponse
Constructs a new RESTErrorResponse object. This method can be used to construct error responses with valid 'statusCode', 'type', 'title' and 'detail'. If you want to omit title or detail, you can pass in
null.Parameters:
- statusCode - The error code of the response. The statusCode parameter should conform to RFC standards for an error.
- type - Type of the error according to RFC 9457. We enforce the following restrictions on top of the RFC: <li>If the provided type is not an absolute URL, it will be prepended with
https://api.commercecloud.salesforce.com/documentation/error/v1/custom-errors/.</li> <li>Custom error types are not allowed to have SYSTEM error type prefix:https://api.commercecloud.salesforce.com/documentation/error/v1/errors/.</li> </ul> - title - Human-readable summary of the error type.
- detail - Human-readable explanation of the specific occurrence of the error.
Returns:
- A new RESTErrorResponse object.
Throws:
- IllegalArgumentException - If the statusCode is not in the (400..599) range or if the error type is not a valid URI or conflicts with SYSTEM error type namespace.
- static createScapiRemoteInclude(apiFamily: String, apiName: String, apiVersion: String, resourcePath: String, params: URLParameter...): RemoteInclude
Constructs a new RemoteInclude object specific for the SCAPI include path. Usage: SCAPI remote include URL have following form:
For the given SCAPI resource path:
RemoteInclude object can be constructed in a script like following:
Please notice that 'BASE_PATH' and 'ORG_ID' are automatically resolved.
Parameters:
- apiFamily - an API Family name. Example: 'product'.
- apiName - an API Name. Example: 'shopper-products'.
- apiVersion - an API Version. Example: 'v1'.
- resourcePath - a Resource path. Example: 'categories/root'
- params - a query parameters (optional)
Returns:
- a new instance of RemoteInclude.
- static createStorefrontControllerRemoteInclude(action: URLAction, params: URLParameter...): RemoteInclude
Constructs a new RemoteInclude object specific for the Storefront Controller include path.
Parameters:
- action - a container to specify target controller. Hostnames in URL actions are ignored.
- params - a query parameters (optional).
Returns:
- a new instance of RemoteInclude.
- static createSuccess(body: Object): RESTSuccessResponse
Constructs a new RESTSuccessResponse object. HTTP status code of the response will be defaulted to 200.
Parameters:
- body - The body of the successful response. This should always be a valid JavaScript JSON object.
Returns:
- A new RESTSuccessResponse object.
- static createSuccess(body: Object, statusCode: Number): RESTSuccessResponse
Constructs a new RESTSuccessResponse object.
Parameters:
- body - The body of the successful response. This should always be a valid JavaScript JSON object.
- statusCode - The http status code of the response. The statusCode parameter should conform to RFC standards for a success.
Returns:
- A new RESTSuccessResponse object.
Throws:
- IllegalArgumentException - If the statusCode is not in the (100..299) range.