Skip to content

Class: StatusCodeError

core.StatusCodeError

An error that will be thrown by fetch when the fetcher response has an HTTP status code of 400 or greater.

This class largely models the StatusCodeError from the (now deprecated) request-promise library, which has a quirky structure.

Example

let response;
try {
  response = await context.fetcher.fetch({
    method: "GET",
    // Open this URL in your browser to see what the data looks like.
    url: "https://api.artic.edu/api/v1/artworks/123",
  });
} catch (error) {
  // If the request failed because the server returned a 300+ status code.
  if (coda.StatusCodeError.isStatusCodeError(error)) {
    // Cast the error as a StatusCodeError, for better intellisense.
    let statusError = error as coda.StatusCodeError;
    // If the API returned an error message in the body, show it to the user.
    let message = statusError.body?.detail;
    if (message) {
      throw new coda.UserVisibleError(message);
    }
  }
  // The request failed for some other reason. Re-throw the error so that it
  // bubbles up.
  throw error;
}

See

Fetching remote data - Errors

Hierarchy

  • Error

StatusCodeError

Properties

body

body: any

The parsed body of the HTTP response.

Defined in

api.ts:160


error

error: any

Alias for body.

Defined in

api.ts:164


name

name: string = 'StatusCodeError'

The name of the error, for identification purposes.

Overrides

Error.name

Defined in

api.ts:152


options

options: FetchRequest

The original fetcher request used to make this HTTP request.

Defined in

api.ts:168


response

response: StatusCodeErrorResponse

The raw HTTP response, including headers.

Defined in

api.ts:172


statusCode

statusCode: number

The HTTP status code, e.g. 404.

Defined in

api.ts:156

Methods

isStatusCodeError

Static isStatusCodeError(err): err is StatusCodeError

Returns if the error is an instance of StatusCodeError. Note that instanceof may not work.

Parameters

Name Type
err any

Returns

err is StatusCodeError

Defined in

api.ts:194