function constructErrorResponse()

in templates/nodejs/src/exceptions.js [55:98]


function constructErrorResponse(response: Object) {
  let body;
  let message;
  let status;
  let headers;

  // Batch request error contains code and body fields
  const isBatchResponse = response.code && response.body;

  if (isBatchResponse) {
    // Handle batch response
    body =
      typeof response.body === 'string'
        ? JSON.parse(response.body)
        : response.body;
    status = response.code;
    message = body.error.message;
    headers = response.headers;
  } else {
    // Handle single response
    if (response.name === STATUS_CODE_ERROR) {
      // Handle when we can get response error code
      body = response.error ? response.error : response;
      body = typeof body === 'string' ? JSON.parse(body) : body;
      // Construct an error message from subfields in body.error
      message = body.error.error_user_msg
        ? `${body.error.error_user_title}: ${body.error.error_user_msg}`
        : body.error.message;
      status = response.statusCode;
      if (response.response) {
        headers = response.response.headers;
      }
    } else if (response.name === REQUEST_ERROR) {
      // Handle network errors e.g. timeout, destination unreachable
      body = {error: response.error};
      // An error message is in the response already
      message = response.message;
      // Network errors have no status code
      status = null;
    }
  }

  return {body, message, status, headers};
}