Class: HttpResponseHandler

HttpResponseHandler()

Receives the HTTP response, and turns it into a JS object. Handles the response failures as well, in which case a corresponding error object is returned via a `Promise`. Only `2xx` response codes count as successful. All other response codes are considered erroneous. By default, expects the input to be a JSON string. Users may choose to customize the behavior by extending this type, and supplying the custom implementation via ClientOptions.

Constructor

new HttpResponseHandler()

Source:

Methods

handle(responsenon-null) → {Promise.<(Object|SpineError)>}

Retrieves the JS object by transforming the contents of the given HTTP response if it was successful, rejects with a respective error otherwise.
Parameters:
Name Type Description
response Response an HTTP request response
Source:
See:
  • parse
Returns:
a promise of a successful server response data, rejected if the client response is not `2xx`, or if the transformation-to-object fails for the response contents.
Type
Promise.<(Object|SpineError)>

onConnectionError(errornon-null) → {Promise.<ConnectionError>}

Obtains the error caught from and erroneous HTTP request, and returns a rejected promise with a given error wrapped into ConnectionError. This handling method differs from others, since it is designed to handle the issues which were caused by an inability to send the HTTP request itself — so in this case there is no HTTP response. Note, that handle is designed to process the HTTP response, including erroneous responses.
Parameters:
Name Type Description
error Error an error which occurred upon sending an HTTP request
Source:
Returns:
a rejected promise with a `ConnectionError`
Type
Promise.<ConnectionError>

(protected) parse(response) → {Promise.<(Object|SpineError)>}

Transforms the response into JS object by parsing the response contents. This implementation expects the response to contain JSON data.
Parameters:
Name Type Description
response an HTTP response
Source:
Returns:
a promise of JS object, or a rejection with the corresponding `SpineError`
Type
Promise.<(Object|SpineError)>

(protected, static) _isClientErrorResponse(statusCodenon-null) → {boolean}

Parameters:
Name Type Description
statusCode number an HTTP request response status code
Source:
Returns:
`true` if the response status code is from 400 to 499, `false` otherwise
Type
boolean

(protected, static) _isServerErrorResponse(statusCodenon-null) → {boolean}

Parameters:
Name Type Description
statusCode number an HTTP request response status code
Source:
Returns:
`true` if the response status code is from 500, `false` otherwise
Type
boolean

(protected, static) _isSuccessfulResponse(statusCodenon-null) → {boolean}

Parameters:
Name Type Description
statusCode number an HTTP request response status code
Source:
Returns:
`true` if the response status code is from 200 to 299, `false` otherwise
Type
boolean