Skip to content

Commit

Permalink
feat: use native AbortError (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jul 15, 2024
1 parent d12e8e5 commit a2c498a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 74 deletions.
58 changes: 1 addition & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"lint": "prettier --check 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*.ts' README.md package.json",
"lint:fix": "prettier --write 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*.ts' README.md package.json",
"pretest": "npm run -s lint",
"test": "vitest run --coverage",
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node16 --module node16 --allowImportingTsExtensions test/typescript-validate.ts"
"test": "vitest",
"validate:ts": "tsc --noEmit --noImplicitAny --target es2023 --esModuleInterop --moduleResolution node16 --module node16 --allowImportingTsExtensions test/typescript-validate.ts"
},
"prettier": {
"plugins": [
Expand Down Expand Up @@ -46,8 +46,7 @@
"dependencies": {
"@octokit/openapi-webhooks-types": "8.3.0",
"@octokit/request-error": "^6.0.1",
"@octokit/webhooks-methods": "^5.0.0",
"aggregate-error": "^5.0.0"
"@octokit/webhooks-methods": "^5.0.0"
},
"devDependencies": {
"@octokit/openapi-webhooks": "8.3.0",
Expand Down
14 changes: 9 additions & 5 deletions src/event-handler/receive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AggregateError from "aggregate-error";
import type {
EmitterWebhookEvent,
State,
Expand Down Expand Up @@ -35,7 +34,7 @@ export function receiverHandle(
const errorHandlers = state.hooks.error || [];

if (event instanceof Error) {
const error = Object.assign(new AggregateError([event]), {
const error = Object.assign(new AggregateError([event], event.message), {
event,
});

Expand All @@ -44,11 +43,13 @@ export function receiverHandle(
}

if (!event || !event.name) {
throw new AggregateError(["Event name not passed"]);
const error = new Error("Event name not passed");
throw new AggregateError([error], error.message);
}

if (!event.payload) {
throw new AggregateError(["Event payload not passed"]);
const error = new Error("Event name not passed");
throw new AggregateError([error], error.message);
}

// flatten arrays of event listeners and remove undefined values
Expand Down Expand Up @@ -83,7 +84,10 @@ export function receiverHandle(
return;
}

const error = new AggregateError(errors) as WebhookEventHandlerError;
const error = new AggregateError(
errors,
errors.map((error) => error.message).join("\n"),
) as WebhookEventHandlerError;
Object.assign(error, {
event,
});
Expand Down
7 changes: 3 additions & 4 deletions src/middleware/node/get-payload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @ts-ignore to address #245
import AggregateError from "aggregate-error";

// remove type imports from http for Deno compatibility
// see https://github.com/octokit/octokit.js/issues/2075#issuecomment-817361886
// import type { IncomingMessage } from "node:http";
Expand Down Expand Up @@ -28,7 +25,9 @@ export function getPayload(request: IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let data: Buffer[] = [];

request.on("error", (error: Error) => reject(new AggregateError([error])));
request.on("error", (error: Error) =>
reject(new AggregateError([error], error.message)),
);
request.on("data", (chunk: Buffer) => data.push(chunk));
request.on("end", () =>
// setImmediate improves the throughput by reducing the pressure from
Expand Down
7 changes: 5 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { EventPayloadMap } from "./generated/webhook-identifiers.js";
import type { Logger } from "./createLogger.js";
import type { EventHandler } from "./event-handler/index.js";
import type { emitterEventNames } from "./generated/webhook-names.js";
import type AggregateError from "aggregate-error";

export type WebhookEventName = keyof EventPayloadMap;
export type ExtractEvents<TEventName> =
Expand Down Expand Up @@ -70,9 +69,13 @@ export interface State extends Options<any> {
*/
export type WebhookError = Error & Partial<RequestError>;

export interface AggregateWebhookError extends AggregateError {
errors: WebhookError[];
}

// todo: rename to "EmitterErrorEvent"
export interface WebhookEventHandlerError<TTransformed = unknown>
extends AggregateError<WebhookError> {
extends AggregateWebhookError {
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed;
Expand Down
3 changes: 1 addition & 2 deletions src/verify-and-receive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AggregateError from "aggregate-error";
import { verify } from "@octokit/webhooks-methods";

import type {
Expand Down Expand Up @@ -36,7 +35,7 @@ export async function verifyAndReceive(
} catch (error: any) {
error.message = "Invalid JSON";
error.status = 400;
throw new AggregateError([error]);
throw new AggregateError([error], error.message);
}

return state.eventHandler.receive({
Expand Down

0 comments on commit a2c498a

Please sign in to comment.