Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prefix error messages with "[@octokit/webhooks] " #339

Merged
merged 2 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/event-handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const { verify } = require('@octokit/webhooks')
const secret = 'mysecret'

if (!verify(secret, request.payload, request.headers['x-hub-signature'])) {
throw new Error('Signature does not match event payload & secret')
throw new Error('[@octokit/webhooks] Signature does not match event payload & secret')
srujandeshpande marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Webhooks<T extends WebhookEvent = WebhookEvent, U = {}> {

constructor(options?: Options<T>) {
if (!options || !options.secret) {
throw new Error("options.secret required");
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State = {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Options, State } from "../types";

export function createMiddleware(options: Options<any>) {
if (!options || !options.secret) {
throw new Error("options.secret required");
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State = {
Expand Down
4 changes: 3 additions & 1 deletion src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export function middleware(

const missingHeaders = getMissingHeaders(request).join(", ");
if (missingHeaders) {
const error = new Error(`Required headers missing: ${missingHeaders}`);
const error = new Error(
`[@octokit/webhooks] Required headers missing: ${missingHeaders}`
);

return state.eventHandler.receive(error).catch(() => {
response.statusCode = 400;
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/verify-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function verifyAndReceive(

if (!matchesSignature) {
const error = new Error(
"signature does not match event payload and secret"
"[@octokit/webhooks] signature does not match event payload and secret"
);

return state.eventHandler.receive(
Expand Down
4 changes: 3 additions & 1 deletion src/verify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function verify(
signature?: string
): boolean {
if (!secret || !eventPayload || !signature) {
throw new TypeError("secret, eventPayload & signature required");
throw new TypeError(
"[@octokit/webhooks] secret, eventPayload & signature required"
);
}

const signatureBuffer = Buffer.from(signature);
Expand Down
6 changes: 3 additions & 3 deletions test/integration/event-handler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test("events", (done) => {
});

eventHandler.on("push", () => {
throw new Error("oops");
throw new Error("[@octokit/webhooks] oops");
srujandeshpande marked this conversation as resolved.
Show resolved Hide resolved
});

return eventHandler.receive({
Expand Down Expand Up @@ -148,11 +148,11 @@ test("multiple errors in same event handler", (done) => {
const eventHandler = createEventHandler({});

eventHandler.on("push", () => {
throw new Error("oops");
throw new Error("[@octokit/webhooks] oops");
});

eventHandler.on("push", () => {
throw new Error("oops");
throw new Error("[@octokit/webhooks] oops");
});

eventHandler
Expand Down
4 changes: 2 additions & 2 deletions test/integration/middleware-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ test("request error", (done) => {
middleware(requestMock, responseMock).then(() => {
expect(responseMock.statusCode).toBe(500);
expect(responseMock.end).toHaveBeenCalledWith(
expect.stringContaining("Error: oops")
expect.stringContaining("Error: [@octokit/webhooks] oops")
);
done();
});

const error = new Error("oops");
const error = new Error("[@octokit/webhooks] oops");
requestMock.emit("error", error);
expect.assertions(2);
});
2 changes: 1 addition & 1 deletion test/integration/server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe("server-test", () => {
const server = http.createServer(api.middleware);

api.on("push", () => {
throw new Error("Oops");
throw new Error("[@octokit/webhooks] Oops");
});

promisify(server.listen.bind(server))(availablePort)
Expand Down
8 changes: 4 additions & 4 deletions test/unit/event-handler-wrap-error-handler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ test("error thrown in error handler", () => {
simple.mock(console, "log", messages.push.bind(messages));
expect(() => {
wrapErrorHandler(() => {
throw new Error("oopsydoopsy");
}, new Error("oops"));
throw new Error("[@octokit/webhooks] oopsydoopsy");
}, new Error(" [@octokit/webhooks]oops"));
}).not.toThrow();

expect(messages.find((message) => /FATAL/.test(message))).toBeTruthy();
Expand All @@ -21,9 +21,9 @@ test("error handler returns rejected Error", () => {

const messages: string[] = [];
simple.mock(console, "log", messages.push.bind(messages));
const promise = Promise.reject(new Error("oopsydoopsy"));
const promise = Promise.reject(new Error("[@octokit/webhooks] oopsydoopsy"));
expect(() =>
wrapErrorHandler(() => promise, new Error("oops"))
wrapErrorHandler(() => promise, new Error("[@octokit/webhooks] oops"))
).not.toThrow();

promise.catch(() => {
Expand Down
6 changes: 4 additions & 2 deletions test/unit/middleware-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ describe("when does a timeout on retrieving the payload", () => {

mockGetMissingHeaders.mockReturnValueOnce([]);
mockGetPayload.mockResolvedValueOnce(undefined);
mockVerifyAndReceive.mockRejectedValueOnce(new Error("random error"));
mockVerifyAndReceive.mockRejectedValueOnce(
new Error("[@octokit/webhooks] random error")
);

const promiseMiddleware = middleware(
{ hooks: {}, path: "/foo" },
Expand All @@ -88,7 +90,7 @@ describe("when does a timeout on retrieving the payload", () => {
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(responseMock.end).toHaveBeenCalledWith("still processing\n");
expect(responseMock.end).not.toHaveBeenCalledWith(
new Error("random error").toString()
new Error("[@octokit/webhooks] random error").toString()
);
});

Expand Down