Skip to content

Commit

Permalink
Export error data
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Jul 26, 2023
1 parent 325f355 commit ce58bc3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
6 changes: 2 additions & 4 deletions server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { authErrorData } from "./error_data.ts";
import { type Payload, verify } from "./deps.ts";
import type { CreationInput } from "./creation.ts";

export const authErrorCode = -32020;

function getJwtFromBearer(headers: Headers): string {
const authHeader = headers.get("Authorization");
if (authHeader === null) {
Expand Down Expand Up @@ -41,11 +40,10 @@ export async function verifyJwt(
} catch (err) {
return {
validationObject: {
code: authErrorCode,
message: "Authorization error",
id: validationObject.id,
data: options.publicErrorStack ? err.stack : undefined,
isError: true,
...authErrorData,
},
methods,
options,
Expand Down
4 changes: 2 additions & 2 deletions server/creation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { internalErrorData } from "./error_data.ts";
import { CustomError } from "./custom_error.ts";
import { verifyJwt } from "./auth.ts";
import type { RpcBatchResponse, RpcResponse } from "../rpc_types.ts";
Expand Down Expand Up @@ -43,11 +44,10 @@ async function executeMethods(
};
}
return {
code: -32603,
message: "Internal error",
id: validationObject.id,
data: options.publicErrorStack ? err.stack : undefined,
isError: true,
...internalErrorData,
};
}
}
Expand Down
19 changes: 19 additions & 0 deletions server/error_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const internalErrorData = { code: -32603, message: "Internal error" };
export const parseErrorData = { code: -32700, message: "Parse error" };
export const methodNotFoundErrorData = {
code: -32601,
message: "Method not found",
};
export const invalidParamsErrorData = {
code: -32602,
message: "Invalid params",
};
export const invalidRequestErrorData = {
code: -32600,
message: "Invalid Request",
};

/**
* -32000 to -32099 Reserved for implementation-defined server-errors.
*/
export const authErrorData = { code: -32020, message: "Authorization error" };
2 changes: 1 addition & 1 deletion server/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./response.ts";
export { authErrorCode } from "./auth.ts";
export * from "./error_data.ts";
18 changes: 10 additions & 8 deletions server/validation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
invalidParamsErrorData,
invalidRequestErrorData,
methodNotFoundErrorData,
parseErrorData,
} from "./error_data.ts";
import { type Methods } from "./response.ts";

import type {
Expand Down Expand Up @@ -61,10 +67,9 @@ function tryToParse(json: string) {
return [JSON.parse(json), null];
} catch {
return [null, {
code: -32700,
message: "Parse error",
id: null,
isError: true,
...parseErrorData,
}];
}
}
Expand Down Expand Up @@ -101,17 +106,15 @@ export function validateRpcRequestObject(
};
} else if (typeof methods[decodedBody.method] !== "function") {
return {
code: -32601,
message: "Method not found",
id: decodedBody.id,
isError: true,
...methodNotFoundErrorData,
};
} else if ("params" in decodedBody && !isRpcParams(decodedBody.params)) {
return {
code: -32602,
message: "Invalid params",
id: decodedBody.id,
isError: true,
...invalidParamsErrorData,
};
} else {
return {
Expand All @@ -123,10 +126,9 @@ export function validateRpcRequestObject(
}
} else {
return {
code: -32600,
message: "Invalid Request",
id: null,
isError: true,
...invalidRequestErrorData,
};
}
}

0 comments on commit ce58bc3

Please sign in to comment.