Skip to content

Commit dfb6559

Browse files
MiniDiggerJRoy
andauthored
feat: catch and return discord errors (#10)
Co-authored-by: Josh Roy <[email protected]>
1 parent 13f3bfa commit dfb6559

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ You can also deploy disgit to docker container:
1212
* Docker Image: The disgit container image is published to the GitHub Container Registry [here](https://github.com/JRoy/disgit/pkgs/container/disgit). For more information on how to authenticate with GitHub's container registry, check the help article [here](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry).
1313

1414
## Environment Variables
15-
digit has the following optional environment variables that you can use to customize your instance;
15+
disgit has the following optional environment variables that you can use to customize your instance;
1616
- `IGNORED_BRANCHES_REGEX` - A regex pattern for branches that should be ignored
1717
- `IGNORED_BRANCHES` - A comma seperated list of branches that should be ignored
1818
- `IGNORED_USERS` - A comma seperated list of users that should be ignored
1919
- `IGNORED_PAYLOADS` - A comma seperated list of webhook events that should be ignored
20+
- `AWAIT_ERRORS` - Set to `true` to await errors from the Discord webhook executor
21+
- `DEBUG_PASTE` - Set to `true` to enable debug embeds.
2022

2123
## Supported Events
2224
The following webhook events are supported as of now;

docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ services:
77
- IGNORED_BRANCHES=${IGNORED_BRANCHES}
88
- IGNORED_USERS=${IGNORED_USERS}
99
- IGNORED_PAYLOADS=${IGNORED_PAYLOADS}
10+
- AWAIT_ERRORS=${AWAIT_ERRORS}
11+
- DEBUG_PASTE=${DEBUG_PASTE}
1012
volumes:
1113
- ./.storage/cache:/worker/cache
1214
ports:

src/env.ts

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export interface Env {
88
IGNORED_PAYLOADS: string;
99

1010
GITHUB_WEBHOOK_SECRET: string;
11+
12+
DEBUG_PASTE: string;
13+
AWAIT_ERRORS: string;
1114
}
1215

1316
/**
@@ -19,6 +22,8 @@ export class BoundEnv {
1922
private ignoredUsers: string[];
2023
private ignoredPayloads: string[];
2124
readonly githubWebhookSecret: string;
25+
readonly debugPaste: boolean;
26+
readonly awaitErrors: boolean;
2227

2328
constructor(env: Env) {
2429
if (typeof env.IGNORED_BRANCHES_REGEX !== 'undefined') {
@@ -28,6 +33,8 @@ export class BoundEnv {
2833
this.ignoredUsers = env.IGNORED_USERS?.split(",") || [];
2934
this.ignoredPayloads = env.IGNORED_PAYLOADS?.split(",") || [];
3035
this.githubWebhookSecret = env.GITHUB_WEBHOOK_SECRET;
36+
this.debugPaste = env.DEBUG_PASTE == "true" || env.DEBUG_PASTE == "1";
37+
this.awaitErrors = env.AWAIT_ERRORS == "true" || env.AWAIT_ERRORS == "1";
3138
}
3239

3340
/**

src/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import {BoundEnv, Env} from './env';
22
import {Sender, shortCommit, truncate} from './util';
33

4-
// If true, will send paste of embed json to discord for debugging
5-
const debug = false;
6-
74
// Handles event sent by cloudflare
85
async function handleRequest(request: Request, env: BoundEnv): Promise<Response> {
96
const event = request.headers.get("X-GitHub-Event");
@@ -26,7 +23,7 @@ async function handleRequest(request: Request, env: BoundEnv): Promise<Response>
2623
return new Response('Webhook NO-OP', {status: 200})
2724
}
2825

29-
if (debug) {
26+
if (env.debugPaste) {
3027
embed = await env.buildDebugPaste(embed);
3128
}
3229

@@ -37,14 +34,21 @@ async function handleRequest(request: Request, env: BoundEnv): Promise<Response>
3734
return new Response('Missing Webhook Authorization', { status: 400 });
3835
}
3936

40-
await fetch(`https://discord.com/api/webhooks/${hookId}/${hookToken}`, {
37+
const promise = fetch(`https://discord.com/api/webhooks/${hookId}/${hookToken}`, {
4138
headers: {
4239
"content-type": "application/json;charset=UTF=8"
4340
},
4441
method: "POST",
4542
body: embed
4643
})
47-
return new Response(`disgit successfully executed webhook ${hookId}`, {status: 200})
44+
45+
let result = env.awaitErrors ? await promise : undefined;
46+
47+
if (result === undefined || result.ok) {
48+
return new Response(`disgit successfully executed webhook ${hookId}`, { status: 200 });
49+
}
50+
51+
return new Response(`Failed to send webhook ${hookId}: Got ${result.status} from discord: ${await result.text()}`, { status: 400 });
4852
} else {
4953
return new Response('Bad Request', { status: 400 })
5054
}

0 commit comments

Comments
 (0)