Errors

Error handling

KelpError, KelpApiError, error codes, network and timeout errors, and cancellation.

The SDK uses a two-level error hierarchy. All SDK errors extend KelpError. Most errors you will handle in practice are KelpApiError.

Error classes

KelpError

Base class for all errors thrown by @kelpclick/client. Extends the native Error. Catch this to handle any SDK error without needing to know the specific subclass.

KelpApiError

Thrown on non-2xx responses, network failures, and request timeouts. Extends KelpError.

class KelpApiError extends KelpError {
  readonly status: number;       // HTTP status code (0 for network/timeout)
  readonly body: unknown;        // parsed response body, if any
  readonly code: KelpErrorCode;  // discriminated error code
  readonly requestId?: string;   // from x-request-id / x-trace-id response header
}

code is one of:

CodeWhen thrown
'auth'HTTP 401 or 403 - invalid or missing API key.
'rate_limit'HTTP 429 - too many requests (after retries are exhausted).
'client'HTTP 4xx other than 401, 403, 429 - bad request, e.g. invalid filter field.
'server'HTTP 5xx - upstream server error (after retries are exhausted).
'network'fetch() rejected - DNS failure, connection refused, CORS, etc.
'timeout'Per-request timeoutMs elapsed before a response arrived.

requestId is set when the server included an x-request-id or x-trace-id response header. Include it in support requests to correlate with server logs.

KelpNotImplementedError

Thrown by SDK methods that have no live server endpoint (suggest and identify on the main client). Extends KelpError.

Handling errors

import { KelpApiError, KelpError, KelpNotImplementedError } from '@kelpclick/client';

try {
  const res = await kelp.search('products', { q: 'shirt' });
} catch (err) {
  if (err instanceof KelpApiError) {
    switch (err.code) {
      case 'auth':
        console.error('Check your API key - is it scoped for search?');
        break;
      case 'rate_limit':
        console.warn('Rate limited. Enable retries or back off.');
        break;
      case 'client':
        console.error('Bad request', err.status, err.body);
        break;
      case 'network':
      case 'timeout':
        console.warn('Transient failure - retry later', err.code);
        break;
      case 'server':
        console.error('Server error', err.status, err.requestId);
        break;
    }
  } else if (err instanceof KelpNotImplementedError) {
    // called suggest() or kelp.identify() - no live endpoint
    console.warn(err.message);
  } else if (err instanceof KelpError) {
    throw err; // unexpected SDK error
  } else {
    throw err; // not from the SDK
  }
}

Network and timeout errors

Both network failures and timeouts surface as KelpApiError rather than raw fetch rejections or AbortError:

  • Network failure (fetch rejected) - code: 'network', status: 0.
  • Timeout (timeoutMs elapsed) - code: 'timeout', status: 0.

This means you can always switch on err.code without checking whether it was a network error or a server error separately.

Enable retries (maxRetries: 2) to automatically recover from 'network', 'timeout', and server-side 429/502/503/504 errors before they reach your catch block. See Transport and retries.

Cancellation

Pass an AbortSignal to cancel a request in flight. Cancelled requests reject with a native DOMException named AbortError - this is not wrapped in KelpApiError:

const controller = new AbortController();

// cancel after 2 s
setTimeout(() => controller.abort(), 2000);

try {
  const res = await kelp.search('products', { q: 'shirt' }, { signal: controller.signal });
} catch (err) {
  if (err instanceof DOMException && err.name === 'AbortError') {
    // request was cancelled - not an error
  }
}

The React hooks (useSearch, useBrowse) handle cancellation automatically. You do not need to manage signals manually when using hooks.

The requestId field

When a request fails with a server error, include err.requestId in any support request. The server sets this from the x-request-id or x-trace-id response header, which maps directly to a server log entry.

} catch (err) {
  if (err instanceof KelpApiError && err.requestId) {
    console.error('Search failed. Request ID for support:', err.requestId);
  }
}

On this page