Skip to content
markpaper

src/signer/errors.test.ts

v0.2.1 · 2.3 KB

Download file
import { describe, expect, it } from 'vitest';
import { extractErrorCode, interpretSignerError, isExchangeRateLimitError, isFarFromMarkError } from './errors.js';

// Texts in the shape the knowledge base documents; the address is a placeholder.
const RATE_TEXT = 'Too Many Requests!: L1Address ratelimit reached 0xYOUR_ADDRESS 40 requests per 60 second is allowed';

describe('extractErrorCode', () => {
  it('reads JSON, key=value and bare known codes', () => {
    expect(extractErrorCode(`{"code":23000,"message":"${RATE_TEXT}"}`)).toBe(23000);
    expect(extractErrorCode('error: code=21706 base amount too small')).toBe(21706);
    expect(extractErrorCode('21734: too far from the mark')).toBe(21734);
    expect(extractErrorCode('nothing here')).toBeUndefined();
  });
});

describe('interpretSignerError', () => {
  it('classifies 23000 by code or by wording and parses the stated limit', () => {
    const byCode = interpretSignerError(`{"code":23000,"message":"${RATE_TEXT}"}`, 502);
    expect(byCode.kind).toBe('rate_limited');
    expect(byCode.code).toBe(23000);
    expect(byCode.rateLimit).toEqual({ requests: 40, windowSec: 60 });
    expect(interpretSignerError(RATE_TEXT).kind).toBe('rate_limited');
    expect(interpretSignerError('Too Many Requests!').kind).toBe('rate_limited');
    expect(isExchangeRateLimitError(RATE_TEXT)).toBe(true);
  });

  it('classifies 21706 and 21734', () => {
    expect(interpretSignerError('code 21706: base amount below minimum').kind).toBe('below_min_base_amount');
    expect(interpretSignerError('order price too far from the mark price').kind).toBe('far_from_mark');
    expect(interpretSignerError('{"code":21734}').kind).toBe('far_from_mark');
    expect(isFarFromMarkError('21734')).toBe(true);
    expect(isFarFromMarkError('21706')).toBe(false);
  });

  it('uses the sidecar HTTP status for its own answers', () => {
    expect(interpretSignerError('timeout', 504).kind).toBe('timeout');
    expect(interpretSignerError('unauthorized', 401).kind).toBe('unauthorized');
    expect(interpretSignerError("bad request: 'price'", 400).kind).toBe('bad_request');
    expect(interpretSignerError('something else', 502)).toMatchObject({
      kind: 'unknown',
      code: undefined,
      rateLimit: undefined,
    });
    expect(interpretSignerError(undefined).message).toBe('');
  });
});
All files