Skip to content

Commit

Permalink
Looser ArangoDB error response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pluma4345 committed Dec 10, 2024
1 parent 49edbcf commit 23b89ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,16 @@ export type ArangoApiResponse<T> = T & ArangoResponseMetadata;
/**
* Indicates whether the given value represents an ArangoDB error response.
*/
export function isArangoErrorResponse(body: any): body is ArangoErrorResponse {
if (!body || typeof body !== 'object') return false;
export function isArangoErrorResponse(
body: unknown,
): body is ArangoErrorResponse {
if (!body || typeof body !== "object") return false;
const obj = body as Record<string, unknown>;
return (
body.error === true &&
typeof body.code === 'number' &&
typeof body.errorMessage === 'string' &&
typeof body.errorNum === 'number'
obj.error === true &&
typeof obj.errorMessage === "string" &&
typeof obj.errorNum === "number" &&
(obj.code === undefined || typeof obj.code === "number")
);
}

Expand All @@ -325,7 +328,7 @@ export type ArangoErrorResponse = {
/**
* Intended response status code as provided in the response body.
*/
code: number;
code?: number;
/**
* Error message as provided in the response body.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class ArangoError extends Error {
/**
* HTTP status code included in the server error response object.
*/
code: number;
code?: number;

/**
* @internal
Expand All @@ -271,7 +271,10 @@ export class ArangoError extends Error {
/**
* Creates a new `ArangoError` from an ArangoDB error response.
*/
constructor(data: connection.ArangoErrorResponse, options: { cause?: Error, isSafeToRetry?: boolean | null } = {}) {
constructor(
data: Omit<connection.ArangoErrorResponse, "error">,
options: { cause?: Error; isSafeToRetry?: boolean | null } = {},
) {
const { isSafeToRetry, ...opts } = options;
super(data.errorMessage, opts);
this.errorNum = data.errorNum;
Expand Down

0 comments on commit 23b89ad

Please sign in to comment.