forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-tojson.test.ts
More file actions
78 lines (71 loc) · 2.87 KB
/
Copy patherror-tojson.test.ts
File metadata and controls
78 lines (71 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { describe, it, expect } from 'vitest';
import {
LilySdkError,
LilyConfigError,
LilyApiError,
LilyAuthenticationError,
LilyTransportError,
isLilySdkError,
LILY_ERROR_CODES,
} from '../src/errors/sdk-error';
/**
* Bounty #60 — $40
* "Add `toJSON()` and richer `toString()` to `LilySdkError`"
*/
describe('LilySdkError toJSON and toString', () => {
it('toJSON returns a plain object with error fields', () => {
const error = new LilyApiError('API failed', {
code: 'API_ERROR',
statusCode: 500,
details: { path: '/v1/test' },
});
const json = error.toJSON();
expect(json).toEqual({
name: 'LilyApiError',
message: 'API failed',
code: 'API_ERROR',
statusCode: 500,
details: { path: '/v1/test' },
});
});
it('toString includes name and message', () => {
const error = new LilyConfigError('Bad config');
expect(error.toString()).toBe('LilyConfigError: Bad config');
});
it('toString includes statusCode when present', () => {
const error = new LilyApiError('API failed', { statusCode: 429 });
expect(error.toString()).toContain('429');
});
it('toString includes code when present', () => {
const error = new LilyApiError('API failed', { code: 'RATE_LIMITED' });
expect(error.toString()).toContain('RATE_LIMITED');
});
it('isLilySdkError works with all subclasses', () => {
expect(isLilySdkError(new LilyConfigError('x'))).toBe(true);
expect(isLilySdkError(new LilyApiError('x'))).toBe(true);
expect(isLilySdkError(new LilyAuthenticationError('x'))).toBe(true);
expect(isLilySdkError(new LilyTransportError('x'))).toBe(true);
expect(isLilySdkError(new LilySdkError('x'))).toBe(true);
expect(isLilySdkError(new Error('x'))).toBe(false);
expect(isLilySdkError('not an error')).toBe(false);
expect(isLilySdkError(null)).toBe(false);
});
it('LILY_ERROR_CODES is frozen', () => {
expect(Object.isFrozen(LILY_ERROR_CODES)).toBe(true);
});
it('LILY_ERROR_CODES has expected values', () => {
expect(LILY_ERROR_CODES.CONFIG_ERROR).toBe('CONFIG_ERROR');
expect(LILY_ERROR_CODES.API_ERROR).toBe('API_ERROR');
expect(LILY_ERROR_CODES.AUTHENTICATION_ERROR).toBe('AUTHENTICATION_ERROR');
expect(LILY_ERROR_CODES.AUTHORIZATION_ERROR).toBe('AUTHORIZATION_ERROR');
expect(LILY_ERROR_CODES.TRANSPORT_ERROR).toBe('TRANSPORT_ERROR');
expect(LILY_ERROR_CODES.VALIDATION_ERROR).toBe('VALIDATION_ERROR');
expect(LILY_ERROR_CODES.NOT_FOUND).toBe('NOT_FOUND');
expect(LILY_ERROR_CODES.CONFLICT).toBe('CONFLICT');
expect(LILY_ERROR_CODES.RATE_LIMITED).toBe('RATE_LIMITED');
expect(LILY_ERROR_CODES.SERVER_ERROR).toBe('SERVER_ERROR');
expect(LILY_ERROR_CODES.TIMEOUT).toBe('TIMEOUT');
expect(LILY_ERROR_CODES.CANCELLED).toBe('CANCELLED');
expect(LILY_ERROR_CODES.RESPONSE_VALIDATION_ERROR).toBe('RESPONSE_VALIDATION_ERROR');
});
});