Class AdapterError

public

Overview

⚠️ This is LEGACY documentation for a feature that is no longer encouraged to be used. If starting a new app or thinking of implementing a new adapter, consider writing a Handler instead to be used with the RequestManager

An AdapterError is used by an adapter to signal that an error occurred during a request to an external API. It indicates a generic error, and subclasses are used to indicate specific error states.

To create a custom error to signal a specific error state in communicating with an external API, extend the AdapterError. For example, if the external API exclusively used HTTP 503 Service Unavailable to indicate it was closed for maintenance:

app/adapters/maintenance-error.js
import AdapterError from '@ember-data/adapter/error';

export default AdapterError.extend({ message: "Down for maintenance." });

This error would then be returned by an adapter's handleResponse method:

app/adapters/application.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import MaintenanceError from './maintenance-error';

export default class ApplicationAdapter extends JSONAPIAdapter {
  handleResponse(status) {
    if (503 === status) {
      return new MaintenanceError();
    }

    return super.handleResponse(...arguments);
  }
}

And can then be detected in an application and used to send the user to an under-maintenance route:

app/routes/application.js
import Route from '@ember/routing/route';
import MaintenanceError from '../adapters/maintenance-error';

export default class ApplicationRoute extends Route {
  actions: {
    error(error, transition) {
      if (error instanceof MaintenanceError) {
        this.transitionTo('under-maintenance');
        return;
      }

      // ...other error handling logic
    }
  }
}