Skip to content

Commit 5fc91b2

Browse files
committed
Centralize error handling
Add source map support to error reporting Add script to update dc-test data
1 parent ef265d7 commit 5fc91b2

10 files changed

Lines changed: 137 additions & 18 deletions

File tree

api/bun.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"mime-types": "^2.1.35",
4242
"parse-http-header": "^1.0.1",
4343
"sort-json": "^2.0.1",
44+
"source-map-support": "^0.5.21",
4445
"xml-js": "^1.6.11"
4546
},
4647
"devDependencies": {

api/src/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import middleware from "./handlers/middleware.ts";
2828
import status from "http-status-codes";
2929
import Honeybadger from "@honeybadger-io/js";
3030
import setupHoneybadger from "./honeybadger-setup.ts";
31+
import { handleError } from "./handlers/error-handler.ts";
3132
import type { AppEnv } from "./types.ts";
33+
import "source-map-support/register";
3234

3335
type ErrorWithResponse = Error & {
3436
response?: {
@@ -48,9 +50,7 @@ app.use("*", async (c, next) => {
4850

4951
app.onError(async (err: ErrorWithResponse, _c) => {
5052
setupHoneybadger(Honeybadger);
51-
if (Honeybadger.config?.enableUncaught) {
52-
await Honeybadger.notifyAsync(err);
53-
}
53+
await handleError(err);
5454

5555
if (err.response?.status) {
5656
return new Response(err.response.body ?? null, {

api/src/handlers/auth/nusso-callback.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { serialize as cookieSerialize } from "cookie";
22
import { dcApiEndpoint } from "../../environment.ts";
33
import { ApiToken } from "../../api/api-token.ts";
4-
import Honeybadger from "@honeybadger-io/js";
4+
import { handleError } from "../error-handler.ts";
55
import type { Context } from "hono";
66
import type { AppEnv } from "../../types.ts";
77
import { getCookie } from "hono/cookie";
@@ -102,9 +102,7 @@ async function redeemSsoToken(
102102
) {
103103
return transform(fillInBlanks({ uid: netid }));
104104
}
105-
await Honeybadger.notifyAsync(err as Error, {
106-
tags: ["auth", "upstream"],
107-
});
105+
await handleError(err as Error);
108106
console.error(errWithData.response?.data);
109107
return null;
110108
}

api/src/handlers/auth/nusso-login.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { baseUrl } from "../../helpers.ts";
22
import { serialize as cookieSerialize } from "cookie";
3-
import Honeybadger from "@honeybadger-io/js";
3+
import { handleError } from "../error-handler.ts";
44
import type { Context } from "hono";
55
import type { AppEnv } from "../../types.ts";
66

@@ -37,9 +37,7 @@ export const handler = async (c: Context<AppEnv>): Promise<Response> => {
3737
},
3838
});
3939
} catch (error) {
40-
await Honeybadger.notifyAsync(error as Error, {
41-
tags: ["auth", "upstream"],
42-
});
40+
await handleError(error as Error);
4341
console.error("NUSSO request error", error);
4442
return new Response(null, { status: 401 });
4543
}

api/src/handlers/error-handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Honeybadger from "@honeybadger-io/js";
2+
3+
export const handleError = async (err: Error) => {
4+
console.error("[api.handleError]", err.stack ?? err);
5+
if (err.cause instanceof Error) {
6+
console.error("[cause]", err.cause.stack ?? err.cause);
7+
}
8+
9+
if (Honeybadger.config?.enableUncaught) {
10+
await Honeybadger.notifyAsync(err);
11+
}
12+
};

api/src/handlers/get-auth-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Honeybadger from "@honeybadger-io/js";
1+
import { handleError } from "./error-handler.ts";
22
import type { Context } from "hono";
33
import { errorMessage } from "../helpers.ts";
44
import type { AppEnv } from "../types.ts";
@@ -38,7 +38,7 @@ export const handler = async (c: Context<AppEnv>): Promise<Response> => {
3838
{ status: 200 },
3939
);
4040
} catch (error) {
41-
await Honeybadger.notifyAsync(error as Error);
41+
await handleError(error as Error);
4242
return new Response("Error verifying API token: " + errorMessage(error), {
4343
status: 401,
4444
headers: { "Content-Type": "text/plain" },

api/src/handlers/get-auth-whoami.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Honeybadger from "@honeybadger-io/js";
1+
import { handleError } from "./error-handler.ts";
22
import type { Context } from "hono";
33
import { errorMessage } from "../helpers.ts";
44
import type { AppEnv } from "../types.ts";
@@ -10,7 +10,7 @@ export const handler = async (c: Context<AppEnv>): Promise<Response> => {
1010
headers: { "content-type": "application/json" },
1111
});
1212
} catch (error) {
13-
await Honeybadger.notifyAsync(error as Error);
13+
await handleError(error as Error);
1414
return new Response("Error verifying API token: " + errorMessage(error), {
1515
status: 401,
1616
});

api/template.yaml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ Conditions:
8989
- ""
9090
DeployChat:
9191
Fn::Equals: [!Ref DeployChat, "true"]
92+
HoneybadgerDisabled:
93+
Fn::Equals:
94+
- !Ref HoneybadgerApiKey
95+
- ""
9296
WriteSecret:
9397
Fn::Equals:
9498
- !Ref WriteConfigSecret
@@ -163,9 +167,22 @@ Resources:
163167
DEFAULT_SEARCH_SIZE: "100"
164168
DEV_TEAM_NET_IDS: !Ref DevTeamNetIds
165169
ENV_PREFIX: !Ref EnvironmentPrefix
166-
HONEYBADGER_API_KEY: !Ref HoneybadgerApiKey
167-
HONEYBADGER_ENV: !Ref HoneybadgerEnv
168-
HONEYBADGER_REVISION: !Ref HoneybadgerRevision
170+
HONEYBADGER_DISABLED: !If [HoneybadgerDisabled, "true", "false"]
171+
HONEYBADGER_API_KEY:
172+
Fn::If:
173+
- HoneybadgerDisabled
174+
- AWS::NoValue
175+
- !Ref HoneybadgerApiKey
176+
HONEYBADGER_ENV:
177+
Fn::If:
178+
- HoneybadgerDisabled
179+
- AWS::NoValue
180+
- !Ref HoneybadgerEnv
181+
HONEYBADGER_REVISION:
182+
Fn::If:
183+
- HoneybadgerDisabled
184+
- AWS::NoValue
185+
- !Ref HoneybadgerRevision
169186
MAGIC_LINK_EMAIL_TEMPLATE: !Ref magicLinkEmailTemplate
170187
PROVIDER_CAPABILITIES: !Ref ProviderCapabilities
171188
READING_ROOM_IPS: !Ref ReadingRoomIPs

bin/refresh_dc_test_data.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
3+
REQUEST_SIZE=${REQUEST_SIZE:-10000}
4+
TEST_PREFIX="dc-test-dc-v2-"
5+
PROD_PREFIX="dc-v2-"
6+
7+
set -e
8+
9+
update_test_index() {
10+
local index=$1
11+
echo "Updating index: $index"
12+
TEST_INDEX=${TEST_PREFIX}${index}
13+
PROD_INDEX=${PROD_PREFIX}${index}
14+
15+
echo -n "Retrieving test IDs..."
16+
ids=$(curl -s "http://localhost:9201/${TEST_INDEX}/_search?size=$REQUEST_SIZE&_source=id" | jq '[.hits.hits[]._id]')
17+
echo "Found $(echo "$ids" | jq 'length') IDs to update."
18+
19+
echo "Building query..."
20+
query=$(jq -nc --argjson ids "$ids" --arg size "$REQUEST_SIZE" '{ "size": $size, "query": { "terms": { "id": $ids } } }')
21+
22+
echo -n "Retrieving docs from production..."
23+
docs=$(curl -s -X POST "http://localhost:9202/${PROD_INDEX}/_search" -H 'Content-Type: application/json' -d "$query")
24+
echo "Retrieved $(echo "$docs" | jq '.hits.hits | length') documents."
25+
26+
echo "Building bulk request..."
27+
timestamp=$(date +%s%3N)
28+
bulk=$(echo "$docs" | jq --arg test_index "$TEST_INDEX-$timestamp" -r '
29+
.hits.hits[] |
30+
({"index": {"_index": $test_index, "_id": ._id}} | tojson),
31+
(._source | tojson)
32+
')
33+
bulk_file=$(mktemp)
34+
echo "$bulk" > "$bulk_file"
35+
36+
echo "Getting index settings..."
37+
settings=$(curl -s "http://localhost:9202/${PROD_INDEX}" | jq '. | to_entries | .[0].value' | jq 'del(.aliases, .settings.index.creation_date, .settings.index.uuid, .settings.index.version, .settings.index.provided_name, .settings.index.default_pipeline)')
38+
39+
if [[ -n "$UPDATE" ]]; then
40+
echo "Creating new index ${TEST_INDEX}-${timestamp}..."
41+
curl -s -X PUT "http://localhost:9201/${TEST_INDEX}-${timestamp}" -H 'Content-Type: application/json' -d "$settings"
42+
echo ""
43+
44+
echo "Adding test data..."
45+
curl -s -X POST "http://localhost:9201/${TEST_INDEX}-${timestamp}/_bulk" \
46+
-H 'Content-Type: application/x-ndjson' \
47+
--data-binary "@$bulk_file"
48+
echo ""
49+
rm "$bulk_file"
50+
51+
echo "Retargeting alias ${TEST_INDEX} to new index..."
52+
old_index=$(curl -s "http://localhost:9201/_alias/${TEST_INDEX}" | jq -r 'keys[0]')
53+
54+
curl -s -X POST 'http://localhost:9201/_aliases' -H 'Content-Type: application/json' -d @- <<EOF
55+
{
56+
"actions": [
57+
{ "remove": { "index": "$old_index", "alias": "${TEST_INDEX}" } },
58+
{ "add": { "index": "${TEST_INDEX}-${timestamp}", "alias": "${TEST_INDEX}" } }
59+
]
60+
}
61+
EOF
62+
echo ""
63+
else
64+
mkdir -p test-data
65+
echo "$bulk" > test-data/$index.ndjson
66+
echo "$settings" > test-data/$index-settings.json
67+
echo "Dry run complete. Re-run with UPDATE=1 to perform the update."
68+
fi
69+
}
70+
71+
setup() {
72+
echo "Setting up..."
73+
PORT=9201 AWS_PROFILE=staging es-proxy start
74+
PORT=9202 AWS_PROFILE=production es-proxy start
75+
}
76+
77+
cleanup() {
78+
echo "Cleaning up..."
79+
PORT=9201 AWS_PROFILE=staging es-proxy stop
80+
PORT=9202 AWS_PROFILE=production es-proxy stop
81+
}
82+
trap cleanup EXIT
83+
setup
84+
for index in collection work file-set; do
85+
update_test_index $index
86+
done

0 commit comments

Comments
 (0)