Skip to content

RequestError ​

DTO that represents a structured backend error. Thrown by every safe runner on RequestBuilder when the response carries a code / error / error_description envelope without nested validation errors.

Importing ​

ts
import { RequestError } from '@basmilius/http-client';

Constructor ​

ts
new RequestError(code: number, error: string, errorDescription: string, statusCode: HttpStatusCode)
ArgumentTypeDescription
codenumberApplication-specific numeric code.
errorstringMachine-readable error key.
errorDescriptionstringHuman-readable description.
statusCodeHttpStatusCodeThe HTTP status code returned by the server.

Properties ​

  • code — number. Read-only.
  • error — string. Read-only.
  • errorDescription — string. Read-only.
  • statusCode — HttpStatusCode. Read-only.

Methods ​

RequestError is decorated with @dto and inherits clone(), fill() and toJSON().

When it is thrown ​

RequestBuilder throws a RequestError when:

  • The response is JSON and matches the code / error / error_description envelope (without errors).
  • The response is non-JSON and the status is not 2xx (a synthetic code: -1, error: 'not_a_json_response' is constructed).
  • RequestBuilder.fetchBlob receives a non-200 response.

Example ​

ts
import { isRequestError, isUnsanctionedRequest } from '@basmilius/http-client';

try {
    await userService.delete(id);
} catch (error) {
    if (!isRequestError(error)) {
        throw error;
    }

    if (isUnsanctionedRequest(error)) {
        redirectToLogin();
        return;
    }

    showToast(error.errorDescription);
}

See also ​