Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions src/api-exception.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
import { ZitadelException } from './zitadel-exception.js';

/**
* Represents an HTTP error returned from an API.
* Represents an HTTP error returned from the Zitadel API.
*
* Exposes the HTTP status code, response headers, and response body.
*/
export class ApiException extends ZitadelException {
/**
* The HTTP body of the server response.
* Can be a decoded JSON object, a string, or null.
* HTTP response headers.
*/
protected readonly _responseBody: Record<string, unknown> | string | null;
private readonly _responseHeaders: Record<string, string[]>;

/**
* The HTTP headers of the server response.
* Represented as a record where keys are header names and values are arrays of strings
* (to accommodate headers that can appear multiple times).
* HTTP response body.
*/
protected readonly _responseHeaders: Record<string, string[]>;
private readonly _responseBody: string | null;

/**
* Constructor.
*
* @param message Error message.
* @param code HTTP status code. This will be passed to ZitadelException's constructor.
* @param responseHeaders HTTP response headers. Defaults to an empty object.
* @param responseBody HTTP response body (decoded JSON object, string, or null). Defaults to null.
* @param code HTTP status code
* @param responseHeaders HTTP response headers
* @param responseBody HTTP response body
*/
public constructor(
message: string,
code: number,
responseHeaders: Record<string, string[]> = {},
responseBody: Record<string, unknown> | string | null = null,
responseHeaders: Record<string, string[]>,
responseBody: string | null,
) {
super(message, undefined, code);
super(`Error ${code}`, undefined, code);
this._responseHeaders = responseHeaders;
this._responseBody = responseBody;
}

/**
* Gets the HTTP status code.
*
* @returns HTTP status code.
*/
public getStatusCode(): number {
return super.getCode();
}

/**
* Gets the HTTP response headers.
*
Expand All @@ -56,11 +43,11 @@ export class ApiException extends ZitadelException {
}

/**
* Gets the HTTP response body (decoded JSON object, string, or null).
* Gets the HTTP response body.
*
* @returns HTTP response body.
*/
public getResponseBody(): Record<string, unknown> | string | null {
public getResponseBody(): string | null {
return this._responseBody;
}
}
4 changes: 2 additions & 2 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ export class BaseAPI {
if (response && response.status >= 200 && response.status < 300) {
return response;
}
const responseBody = await response.text();
throw new ApiException(
'Response returned an error code',
response.status,
Object.fromEntries(
Object.entries(response.headers).map(([k, v]) => [
k,
Array.isArray(v) ? v : [v],
]),
),
response?.body?.toString(),
responseBody,
);
}

Expand Down
4 changes: 2 additions & 2 deletions test/api-exception.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ApiException } from '../src/api-exception.js';

describe('ApiExceptionTest', () => {
test('testApiException', () => {
const e = new ApiException('Error 418', 418, { H: ['v'] }, 'body');
const e = new ApiException(418, { H: ['v'] }, 'body');
expect(e.message).toBe('Error 418');
expect(e.getStatusCode()).toBe(418);
expect(e.getCode()).toBe(418);
expect(e.getResponseHeaders()).toEqual({ H: ['v'] });
expect(e.getResponseBody()).toBe('body');
});
Expand Down
Loading