Skip to content

Latest commit

 

History

History
171 lines (139 loc) · 3.59 KB

error_creators.md

File metadata and controls

171 lines (139 loc) · 3.59 KB

Error creators

Farfetched uses its own object to represent errors for different reasons. To create these errors, Farfetched provides a set of error creators.

All error creators are functions that take an object with a configuration of an error and return an internal representation of the error.

:::tip All errors are serializable, so they can be safely transmitted through network. :::

E.g., you can use error creators to create your own factory and want to reuse existing error handling infrastructure in Farfetched, or to imitate these errors in tests.

invalidDataError

InvalidDataError is thrown when the response data is invalid against the given Contract or Validator.

import { invalidDataError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw invalidDataError({
            validationErrors: ['Test error'],
            response: {},
          });
        }),
      ],
    ],
  });
});

timeoutError

TimeoutError is thrown when the query takes too long to complete.

import { timeoutError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw timeoutError({
            timeout: 10,
          });
        }),
      ],
    ],
  });
});

abortError

AbortError is thrown when the query is aborted.

import { abortError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw abortError();
        }),
      ],
    ],
  });
});

preparationError

Preparation error is thrown when the response cannot be prepared for some reason. For example, when the response is not a JSON string, but supposed to be.

import { preparationError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw preparationError({ reason: 'Weird JSON', response: '{lolkek' });
        }),
      ],
    ],
  });
});

httpError

HttpError is thrown when the response status code is not 2xx.

import { httpError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw httpError({
            status: 429,
            statusText: 'I am teapot',
            response: null,
          });
        }),
      ],
    ],
  });
});

networkError

NetworkError is thrown when the query fails because of network problems.

import { networkError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw networkError({
            reason: 'Can not',
          });
        }),
      ],
    ],
  });
});

configurationError

ConfigurationError is thrown when the query is misconfigured. E.g., when the URL is not URL.

import { configurationError } from '@farfetched/core';

test('on error', async () => {
  const scope = fork({
    handlers: [
      [
        query.__.executeFx,
        vi.fn(() => {
          throw configurationError({
            validationErrors: ['"LOL KEK" is not valid URL'],
          });
        }),
      ],
    ],
  });
});