Skip to content

Type Alias: FetchInterceptorError

FetchInterceptorError = Interceptor<FetchError, FetchArgsInterceptor>

Defined in: packages/fetch/src/types/interceptors.ts:56

Fetch error interceptor to be invoked whenever an exception occurs. This interceptor can also be used as the error transformer by returning FetchError.

Note: The error interceptor is only triggered if the request ultimately fails. If retries are enabled (retry > 0) and a subsequent attempt succeeds, this interceptor will not be invoked for the intermediate failures.

Param

custom error that also contain URL, options & response

Returns

returning undefined or not returning anything will not override the error

Examples

Intercept fetch errors to log errors

javascript
import fetch from '@superutils/fetch'

// not returning anything or returning undefined will avoid transforming the error.
const logError = fetchErr => console.log(fetchErr)
const result = await fetch.get('[DUMMYJSON-DOT-COM]/http/400', {
  interceptors: {
    error: [logError]
  }
})

Intercept & transform fetch errors

javascript
import fetch from '@superutils/fetch'

// Interceptors can be async functions or just return a promise that resolves to the error.
// If the execution of the interceptor fails or promise rejects, it will be ignored.
// To transform the error it must directly return an error or a Promise that `resolves` with an error.
const transformError = async (fetchErr, url, options) => {
  fetchErr.message = 'Custom errormessage'
	 return Promise.resolve(fetchErr)
}
const result = await fetch.get('[DUMMYJSON-DOT-COM]/http/400', {
  interceptors: {
    error: [transformError]
  }
})