Skip to content

Commit

Permalink
update errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ptpaterson committed Apr 17, 2024
1 parent d25d0f3 commit d467ade
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 142 deletions.
8 changes: 4 additions & 4 deletions __tests__/integration/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ describe("StreamClient", () => {
}
} catch (e: any) {
expect(e).toBeInstanceOf(AbortError);
expect(e.httpStatus).toBe(-1);
expect(e.httpStatus).toBeUndefined();
expect(e.abort).toBe("oops");
} finally {
stream?.close();
Expand All @@ -316,7 +316,7 @@ describe("StreamClient", () => {
}
} catch (e: any) {
expect(e).toBeInstanceOf(QueryRuntimeError);
expect(e.httpStatus).toBe(-1);
expect(e.httpStatus).toBeUndefined();
} finally {
stream?.close();
}
Expand Down Expand Up @@ -347,7 +347,7 @@ describe("StreamClient", () => {
function onEvent(_) {},
function onError(e) {
if (e instanceof AbortError) {
expect(e.httpStatus).toBe(-1);
expect(e.httpStatus).toBeUndefined();
expect(e.abort).toBe("oops");
}
resolve();
Expand Down Expand Up @@ -382,7 +382,7 @@ describe("StreamClient", () => {
function onEvent(_) {},
function onError(e) {
if (e instanceof QueryRuntimeError) {
expect(e.httpStatus).toBe(-1);
expect(e.httpStatus).toBeUndefined();
}
resolve();
},
Expand Down
114 changes: 61 additions & 53 deletions __tests__/unit/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,74 @@ import {
AbortError,
AuthenticationError,
AuthorizationError,
ConstraintFailureError,
ContendedTransactionError,
InvalidRequestError,
QueryCheckError,
QueryFailure,
QueryRuntimeError,
QueryTimeoutError,
ServiceError,
ServiceInternalError,
ServiceTimeoutError,
ThrottlingError,
} from "../../src";
import { getServiceError } from "../../src/errors";

describe("query", () => {
it.each`
httpStatus | code | errorClass
${400} | ${"invalid_function_definition"} | ${QueryCheckError}
${400} | ${"invalid_identifier"} | ${QueryCheckError}
${400} | ${"invalid_query"} | ${QueryCheckError}
${400} | ${"invalid_syntax"} | ${QueryCheckError}
${400} | ${"invalid_type"} | ${QueryCheckError}
${400} | ${"unbound_variable"} | ${QueryRuntimeError}
${400} | ${"index_out_of_bounds"} | ${QueryRuntimeError}
${400} | ${"type_mismatch"} | ${QueryRuntimeError}
${400} | ${"invalid_argument"} | ${QueryRuntimeError}
${400} | ${"invalid_bounds"} | ${QueryRuntimeError}
${400} | ${"invalid_regex"} | ${QueryRuntimeError}
${400} | ${"constraint_failure"} | ${QueryRuntimeError}
${400} | ${"invalid_schema"} | ${QueryRuntimeError}
${400} | ${"invalid_document_id"} | ${QueryRuntimeError}
${400} | ${"document_id_exists"} | ${QueryRuntimeError}
${400} | ${"document_not_found"} | ${QueryRuntimeError}
${400} | ${"document_deleted"} | ${QueryRuntimeError}
${400} | ${"invalid_function_invocation"} | ${QueryRuntimeError}
${400} | ${"invalid_index_invocation"} | ${QueryRuntimeError}
${400} | ${"null_value"} | ${QueryRuntimeError}
${400} | ${"invalid_null_access"} | ${QueryRuntimeError}
${400} | ${"invalid_cursor"} | ${QueryRuntimeError}
${400} | ${"permission_denied"} | ${QueryRuntimeError}
${400} | ${"invalid_effect"} | ${QueryRuntimeError}
${400} | ${"invalid_write"} | ${QueryRuntimeError}
${400} | ${"internal_failure"} | ${QueryRuntimeError}
${400} | ${"divide_by_zero"} | ${QueryRuntimeError}
${400} | ${"invalid_id"} | ${QueryRuntimeError}
${400} | ${"invalid_secret"} | ${QueryRuntimeError}
${400} | ${"invalid_time"} | ${QueryRuntimeError}
${400} | ${"invalid_unit"} | ${QueryRuntimeError}
${400} | ${"invalid_date"} | ${QueryRuntimeError}
${400} | ${"limit_exceeded"} | ${QueryRuntimeError}
${400} | ${"stack_overflow"} | ${QueryRuntimeError}
${400} | ${"invalid_computed_field_access"} | ${QueryRuntimeError}
${400} | ${"disabled_feature"} | ${QueryRuntimeError}
${400} | ${"invalid_receiver"} | ${QueryRuntimeError}
${400} | ${"invalid_timestamp_field_access"} | ${QueryRuntimeError}
${400} | ${"invalid_request"} | ${InvalidRequestError}
${400} | ${"abort"} | ${AbortError}
${401} | ${"unauthorized"} | ${AuthenticationError}
${403} | ${"forbidden"} | ${AuthorizationError}
${409} | ${"contended_transaction"} | ${ContendedTransactionError}
${429} | ${"throttle"} | ${ThrottlingError}
${440} | ${"time_out"} | ${QueryTimeoutError}
${503} | ${"time_out"} | ${ServiceTimeoutError}
${500} | ${"internal_error"} | ${ServiceInternalError}
${999} | ${"error_not_yet_subclassed_in_client"} | ${ServiceError}
${-1} | ${"error_not_yet_subclassed_in_client"} | ${ServiceError}
httpStatus | code | errorClass
${400} | ${"invalid_query"} | ${QueryCheckError}
${400} | ${"unbound_variable"} | ${QueryRuntimeError}
${400} | ${"index_out_of_bounds"} | ${QueryRuntimeError}
${400} | ${"type_mismatch"} | ${QueryRuntimeError}
${400} | ${"invalid_argument"} | ${QueryRuntimeError}
${400} | ${"invalid_bounds"} | ${QueryRuntimeError}
${400} | ${"invalid_regex"} | ${QueryRuntimeError}
${400} | ${"invalid_schema"} | ${QueryRuntimeError}
${400} | ${"invalid_document_id"} | ${QueryRuntimeError}
${400} | ${"document_id_exists"} | ${QueryRuntimeError}
${400} | ${"document_not_found"} | ${QueryRuntimeError}
${400} | ${"document_deleted"} | ${QueryRuntimeError}
${400} | ${"invalid_function_invocation"} | ${QueryRuntimeError}
${400} | ${"invalid_index_invocation"} | ${QueryRuntimeError}
${400} | ${"null_value"} | ${QueryRuntimeError}
${400} | ${"invalid_null_access"} | ${QueryRuntimeError}
${400} | ${"invalid_cursor"} | ${QueryRuntimeError}
${400} | ${"permission_denied"} | ${QueryRuntimeError}
${400} | ${"invalid_effect"} | ${QueryRuntimeError}
${400} | ${"invalid_write"} | ${QueryRuntimeError}
${400} | ${"internal_failure"} | ${QueryRuntimeError}
${400} | ${"divide_by_zero"} | ${QueryRuntimeError}
${400} | ${"invalid_id"} | ${QueryRuntimeError}
${400} | ${"invalid_secret"} | ${QueryRuntimeError}
${400} | ${"invalid_time"} | ${QueryRuntimeError}
${400} | ${"invalid_unit"} | ${QueryRuntimeError}
${400} | ${"invalid_date"} | ${QueryRuntimeError}
${400} | ${"limit_exceeded"} | ${QueryRuntimeError}
${400} | ${"stack_overflow"} | ${QueryRuntimeError}
${400} | ${"invalid_computed_field_access"} | ${QueryRuntimeError}
${400} | ${"disabled_feature"} | ${QueryRuntimeError}
${400} | ${"invalid_receiver"} | ${QueryRuntimeError}
${400} | ${"invalid_timestamp_field_access"} | ${QueryRuntimeError}
${400} | ${"invalid_request"} | ${InvalidRequestError}
${400} | ${"abort"} | ${AbortError}
${400} | ${"constraint_failure"} | ${ConstraintFailureError}
${401} | ${"unauthorized"} | ${AuthenticationError}
${403} | ${"forbidden"} | ${AuthorizationError}
${409} | ${"contended_transaction"} | ${ContendedTransactionError}
${429} | ${"throttle"} | ${ThrottlingError}
${440} | ${"time_out"} | ${QueryTimeoutError}
${503} | ${"time_out"} | ${QueryTimeoutError}
${500} | ${"internal_error"} | ${ServiceInternalError}
${400} | ${"some unhandled code"} | ${QueryRuntimeError}
${401} | ${"some unhandled code"} | ${QueryRuntimeError}
${403} | ${"some unhandled code"} | ${QueryRuntimeError}
${409} | ${"some unhandled code"} | ${QueryRuntimeError}
${429} | ${"some unhandled code"} | ${QueryRuntimeError}
${440} | ${"some unhandled code"} | ${QueryRuntimeError}
${500} | ${"some unhandled code"} | ${QueryRuntimeError}
${503} | ${"some unhandled code"} | ${QueryRuntimeError}
${999} | ${"some unhandled code"} | ${QueryRuntimeError}
${undefined} | ${"some unhandled code"} | ${QueryRuntimeError}
`(
"QueryFailures with status '$httpStatus' and code '$code' are correctly mapped to $errorClass",
({ httpStatus, code, errorClass }) => {
Expand All @@ -75,14 +78,19 @@ describe("query", () => {
message: "error message",
code,
abort: "oops",
constraint_failures: [{ message: "oops" }],
},
};

const error = getServiceError(failure, httpStatus);

expect(error).toBeInstanceOf(errorClass);
expect(error.httpStatus).toEqual(httpStatus);
expect(error.code).toEqual(code);

const error_no_status = getServiceError(failure);
expect(error_no_status).toBeInstanceOf(errorClass);
expect(error_no_status.httpStatus).toBeUndefined();
expect(error_no_status.code).toEqual(code);
},
);
});
5 changes: 2 additions & 3 deletions __tests__/unit/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
QueryTimeoutError,
ServiceError,
ServiceInternalError,
ServiceTimeoutError,
ThrottlingError,
} from "../../src";
import { getClient, getDefaultHTTPClientOptions } from "../client";
Expand Down Expand Up @@ -38,7 +37,7 @@ describe("query", () => {
${999} | ${ServiceError} | ${{ code: "error_not_yet_subclassed_in_client", message: "who knows!!!" }}
${429} | ${ThrottlingError} | ${{ code: "throttle", message: "too much" }}
${500} | ${ServiceInternalError} | ${{ code: "internal_error", message: "unexpected error" }}
${503} | ${ServiceTimeoutError} | ${{ code: "time_out", message: "too slow on our side" }}
${503} | ${QueryTimeoutError} | ${{ code: "time_out", message: "too slow on our side" }}
`(
"throws an $expectedErrorType on a $httpStatus",
async ({ httpStatus, expectedErrorType, expectedErrorFields }) => {
Expand Down Expand Up @@ -67,7 +66,7 @@ describe("query", () => {
${999} | ${ServiceError} | ${{ code: "error_not_yet_subclassed_in_client", message: "who knows!!!" }}
${429} | ${ThrottlingError} | ${{ code: "throttle", message: "too much" }}
${500} | ${ServiceInternalError} | ${{ code: "internal_error", message: "unexpected error" }}
${503} | ${ServiceTimeoutError} | ${{ code: "time_out", message: "too slow on our side" }}
${503} | ${QueryTimeoutError} | ${{ code: "time_out", message: "too slow on our side" }}
`(
"Includes a summary when not present in error field but present at top-level",
async ({ httpStatus, expectedErrorType, expectedErrorFields }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ export class StreamClient<T extends QueryValue = any> {
if (deserializedEvent.type === "error") {
// Errors sent from Fauna are assumed fatal
this.close();
throw getServiceError(deserializedEvent, -1);
throw getServiceError(deserializedEvent);
}

this.#last_ts = deserializedEvent.txn_ts;
Expand Down
Loading

0 comments on commit d467ade

Please sign in to comment.