Skip to content

Commit 56173a2

Browse files
committed
refactor: simplify error messages - use error field or HTTP status text
1 parent 0cfb15d commit 56173a2

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/error.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
/**
22
* Helper function to create error message from API response
33
*/
4+
import { STATUS_CODES } from 'http';
5+
46
export function createErrorMessage(statusCode: number, body: string): string {
57
try {
68
const parsed = JSON.parse(body);
7-
const errorField = parsed.error || JSON.stringify(parsed);
8-
return `${statusCode}: ${errorField}`;
9+
if (parsed.error) {
10+
return parsed.error;
11+
}
912
} catch {
10-
return `${statusCode}: ${body}`;
13+
// Body is not JSON, will use status text
1114
}
15+
16+
return STATUS_CODES[statusCode] || `HTTP Error ${statusCode}`;
1217
}

tests/client.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,32 @@ describe('Error handling', () => {
4141

4242
it('should create error message from API response with JSON error field', () => {
4343
const message = createErrorMessage(400, JSON.stringify({ error: 'INVALID_PARAMETER' }));
44-
expect(message).toContain('400');
45-
expect(message).toContain('INVALID_PARAMETER');
44+
expect(message).toBe('INVALID_PARAMETER');
4645
});
4746

4847
it('should create error message from API response with plain text', () => {
4948
const message = createErrorMessage(500, 'Internal Server Error');
50-
expect(message).toContain('500');
51-
expect(message).toContain('Internal Server Error');
49+
expect(message).toBe('Internal Server Error');
5250
});
5351

5452
it('should create error message from API response with malformed JSON', () => {
5553
const message = createErrorMessage(500, '{broken json}');
56-
expect(message).toContain('500');
57-
expect(message).toContain('{broken json}');
54+
expect(message).toBe('Internal Server Error');
5855
});
5956

6057
it('should create error message with status code and full JSON object', () => {
6158
const message = createErrorMessage(400, JSON.stringify({ code: 'ERR_001', detail: 'Bad request' }));
62-
expect(message).toContain('400');
63-
expect(message).toContain('code');
59+
expect(message).toBe('Bad Request');
6460
});
6561

6662
it('should create error message with nested error object', () => {
6763
const message = createErrorMessage(403, JSON.stringify({ error: 'UNAUTHORIZED', details: 'Invalid token' }));
68-
expect(message).toContain('403');
69-
expect(message).toContain('UNAUTHORIZED');
64+
expect(message).toBe('UNAUTHORIZED');
7065
});
7166

7267
it('should handle empty string body', () => {
7368
const message = createErrorMessage(500, '');
74-
expect(message).toContain('500');
69+
expect(message).toBe('Internal Server Error');
7570
});
7671

7772
it('should handle network error messages', () => {

0 commit comments

Comments
 (0)