Skip to content

Commit

Permalink
Change error constructor signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
pluma4345 committed Nov 28, 2024
1 parent 62d17e9 commit a2a07ca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ This driver uses semantic versioning:

- Renamed `CollectionDropOptions` type to `DropCollectionOptions`
- Renamed `CollectionTruncateOptions` type to `TruncateCollectionOptions`
- Changed error type constructor signatures

The `request` property is now always positional and the `options` property
is always optional.

## [10.0.0-alpha.0] - 2024-11-28

Expand Down
35 changes: 23 additions & 12 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ interface SystemError extends Error {
export class PropagationTimeoutError extends Error {
name = "PropagationTimeoutError";

constructor(message: string | undefined, options: { cause: Error }) {
constructor(message?: string, options: { cause?: Error } = {}) {
super(message ?? 'Timed out while waiting for propagation', options);
}
}
Expand All @@ -196,8 +196,8 @@ export class NetworkError extends Error {
*/
request: globalThis.Request;

constructor(message: string, options: { request: globalThis.Request, cause?: Error, isSafeToRetry?: boolean | null }) {
const { request, isSafeToRetry = null, ...opts } = options;
constructor(message: string, request: globalThis.Request, options: { cause?: Error, isSafeToRetry?: boolean | null } = {}) {
const { isSafeToRetry = null, ...opts } = options;
super(message, opts);
this.request = request;
this.isSafeToRetry = isSafeToRetry;
Expand All @@ -219,8 +219,8 @@ export class NetworkError extends Error {
export class ResponseTimeoutError extends NetworkError {
name = "ResponseTimeoutError";

constructor(message: string | undefined, options: { request: globalThis.Request, cause?: Error, isSafeToRetry?: boolean | null }) {
super(message ?? 'Timed out while waiting for server response', options);
constructor(message: string | undefined, request: globalThis.Request, options: { cause?: Error, isSafeToRetry?: boolean | null } = {}) {
super(message ?? 'Timed out while waiting for server response', request, options);
}
}

Expand All @@ -230,8 +230,8 @@ export class ResponseTimeoutError extends NetworkError {
export class RequestAbortedError extends NetworkError {
name = "RequestAbortedError";

constructor(message: string | undefined, options: { request: globalThis.Request, cause?: Error, isSafeToRetry?: boolean | null }) {
super(message ?? 'Request aborted', options);
constructor(message: string | undefined, request: globalThis.Request, options: { cause?: Error, isSafeToRetry?: boolean | null } = {}) {
super(message ?? 'Request aborted', request, options);
}
}

Expand All @@ -243,17 +243,17 @@ export class RequestAbortedError extends NetworkError {
export class FetchFailedError extends NetworkError {
name = "FetchFailedError";

constructor(message: string | undefined, options: { request: globalThis.Request, cause: TypeError, isSafeToRetry?: boolean | null }) {
constructor(message: string | undefined, request: globalThis.Request, options: { cause?: TypeError, isSafeToRetry?: boolean | null } = {}) {
let isSafeToRetry = options.isSafeToRetry;
if (options.cause.cause instanceof Error) {
if (options.cause?.cause instanceof Error) {
if (isSafeToRetry === undefined) {
isSafeToRetry = isSafeToRetryFailedFetch(options.cause.cause) || undefined;
}
if (message === undefined) {
message = `Fetch failed: ${options.cause.cause.message}`;
}
}
super(message ?? 'Fetch failed', { ...options, isSafeToRetry });
super(message ?? 'Fetch failed', request, { ...options, isSafeToRetry });
}
}

Expand All @@ -278,7 +278,7 @@ export class HttpError extends NetworkError {
*/
constructor(response: connection.ProcessedResponse, options: { cause?: Error, isSafeToRetry?: boolean | null } = {}) {
const message = messages[response.status] ?? messages[500];
super(message, { ...options, request: response.request });
super(message, response.request, options);
this.response = response;
this.code = response.status;
}
Expand All @@ -305,6 +305,13 @@ export class ArangoError extends Error {
*/
isSafeToRetry: boolean | null = null;

/**
* @internal
*/
get error(): true {
return true;
}

/**
* ArangoDB error code.
*
Expand Down Expand Up @@ -338,7 +345,7 @@ export class ArangoError extends Error {
/**
* Creates a new `ArangoError` from an ArangoDB error response.
*/
constructor(data: ArangoErrorResponse, options: { cause?: Error, isSafeToRetry?: boolean | null }) {
constructor(data: ArangoErrorResponse, options: { cause?: Error, isSafeToRetry?: boolean | null } = {}) {
const { isSafeToRetry, ...opts } = options;
super(data.errorMessage, opts);
this.errorNum = data.errorNum;
Expand Down Expand Up @@ -391,4 +398,8 @@ export class ArangoError extends Error {
code: this.code,
};
}

toString() {
return `${this.name} ${this.errorNum}: ${this.message}`;
}
}
8 changes: 4 additions & 4 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ export function createRequest(
if (signal.aborted) {
const reason = typeof signal.reason == 'string' ? signal.reason : undefined;
if (reason === REASON_TIMEOUT) {
error = new ResponseTimeoutError(undefined, { request });
error = new ResponseTimeoutError(undefined, request, { cause });
} else {
error = new RequestAbortedError(reason, { request, cause });
error = new RequestAbortedError(reason, request, { cause });
}
} else if (cause instanceof TypeError) {
error = new FetchFailedError(undefined, { request, cause });
error = new FetchFailedError(undefined, request, { cause });
} else {
error = new NetworkError(cause.message, { request, cause });
error = new NetworkError(cause.message, request, { cause });
}
if (config.afterResponse) {
const p = config.afterResponse(error);
Expand Down

0 comments on commit a2a07ca

Please sign in to comment.