Skip to content

Commit 9eebad8

Browse files
committed
Add thrown 500 redirects.
1 parent 7e197cc commit 9eebad8

File tree

4 files changed

+147
-10
lines changed

4 files changed

+147
-10
lines changed
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import { ActionFunction, json } from '@remix-run/node';
1+
import { ActionFunction, json, redirect, LoaderFunction } from '@remix-run/node';
22
import { useActionData } from '@remix-run/react';
33

4+
export const loader: LoaderFunction = async ({ params: { id } }) => {
5+
if (id === '-1') {
6+
throw new Error('Unexpected Server Error from Loader');
7+
}
8+
};
9+
410
export const action: ActionFunction = async ({ params: { id } }) => {
511
if (id === '-1') {
6-
throw new Error('Error');
12+
throw new Error('Unexpected Server Error');
13+
}
14+
15+
if (id === '-2') {
16+
// Note: This GET request triggers to the `Loader` of the URL, not the `Action`.
17+
throw redirect('/action-json-response/-1');
718
}
819

920
return json({ test: 'test' });
1021
};
1122

1223
export default function ActionJSONResponse() {
13-
const { test } = useActionData();
24+
const data = useActionData();
25+
1426
return (
1527
<div>
16-
<h1>{test}</h1>
28+
<h1>{data && data.test ? data.test : 'Not Found'}</h1>
1729
</div>
1830
);
1931
}

packages/remix/test/integration/app/routes/loader-json-response/$id.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { json, LoaderFunction } from '@remix-run/node';
1+
import { json, LoaderFunction, redirect } from '@remix-run/node';
22
import { useLoaderData } from '@remix-run/react';
33

44
type LoaderData = { id: string };
55

66
export const loader: LoaderFunction = async ({ params: { id } }) => {
7+
if (id === '-2') {
8+
throw new Error('Unexpected Server Error from Loader');
9+
}
10+
711
if (id === '-1') {
8-
throw new Error('Error');
12+
throw redirect('/loader-json-response/-2');
913
}
1014

1115
return json({
@@ -18,7 +22,7 @@ export default function LoaderJSONResponse() {
1822

1923
return (
2024
<div>
21-
<h1>{data.id}</h1>
25+
<h1>{data && data.id ? data.id : 'Not Found'}</h1>
2226
</div>
2327
);
2428
}

packages/remix/test/integration/test/server/action.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ describe('Remix API Actions', () => {
2222
description: 'routes/action-json-response/$id',
2323
op: 'remix.server.action',
2424
},
25+
{
26+
description: 'routes/action-json-response/$id',
27+
op: 'remix.server.loader',
28+
},
2529
{
2630
description: 'routes/action-json-response/$id',
2731
op: 'remix.server.documentRequest',
@@ -52,7 +56,7 @@ describe('Remix API Actions', () => {
5256
values: [
5357
{
5458
type: 'Error',
55-
value: 'Error',
59+
value: 'Unexpected Server Error',
5660
stacktrace: expect.any(Object),
5761
mechanism: {
5862
data: {
@@ -66,4 +70,62 @@ describe('Remix API Actions', () => {
6670
},
6771
});
6872
});
73+
74+
it('handles a thrown 500 response', async () => {
75+
const baseURL = await runServer();
76+
const url = `${baseURL}/action-json-response/-2`;
77+
78+
const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest(url, 3, 'post');
79+
80+
assertSentryTransaction(transaction_1[2], {
81+
contexts: {
82+
trace: {
83+
op: 'http.server',
84+
status: 'ok',
85+
tags: {
86+
method: 'POST',
87+
'http.status_code': '302',
88+
},
89+
},
90+
},
91+
tags: {
92+
transaction: 'routes/action-json-response/$id',
93+
},
94+
});
95+
96+
assertSentryTransaction(transaction_2[2], {
97+
contexts: {
98+
trace: {
99+
op: 'http.server',
100+
status: 'internal_error',
101+
tags: {
102+
method: 'GET',
103+
'http.status_code': '500',
104+
},
105+
},
106+
},
107+
tags: {
108+
transaction: 'routes/action-json-response/$id',
109+
},
110+
});
111+
112+
assertSentryEvent(event[2], {
113+
exception: {
114+
values: [
115+
{
116+
type: 'Error',
117+
value: 'Unexpected Server Error from Loader',
118+
stacktrace: expect.any(Object),
119+
mechanism: {
120+
data: {
121+
function: 'loader',
122+
},
123+
handled: true,
124+
type: 'instrument',
125+
},
126+
},
127+
],
128+
},
129+
});
130+
});
69131
});

packages/remix/test/integration/test/server/loader.test.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Remix API Loaders', () => {
2828

2929
it('reports an error thrown from the loader', async () => {
3030
const baseURL = await runServer();
31-
const url = `${baseURL}/loader-json-response/-1`;
31+
const url = `${baseURL}/loader-json-response/-2`;
3232

3333
const [transaction, event] = await getMultipleEnvelopeRequest(url, 2);
3434

@@ -48,7 +48,7 @@ describe('Remix API Loaders', () => {
4848
values: [
4949
{
5050
type: 'Error',
51-
value: 'Error',
51+
value: 'Unexpected Server Error from Loader',
5252
stacktrace: expect.any(Object),
5353
mechanism: {
5454
data: {
@@ -86,4 +86,63 @@ describe('Remix API Loaders', () => {
8686
],
8787
});
8888
});
89+
90+
it('handles a thrown 500 response', async () => {
91+
const baseURL = await runServer();
92+
const url = `${baseURL}/loader-json-response/-1`;
93+
94+
const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest(url, 3);
95+
debugger;
96+
97+
assertSentryTransaction(transaction_1[2], {
98+
contexts: {
99+
trace: {
100+
op: 'http.server',
101+
status: 'ok',
102+
tags: {
103+
method: 'GET',
104+
'http.status_code': '302',
105+
},
106+
},
107+
},
108+
tags: {
109+
transaction: 'routes/loader-json-response/$id',
110+
},
111+
});
112+
113+
assertSentryTransaction(transaction_2[2], {
114+
contexts: {
115+
trace: {
116+
op: 'http.server',
117+
status: 'internal_error',
118+
tags: {
119+
method: 'GET',
120+
'http.status_code': '500',
121+
},
122+
},
123+
},
124+
tags: {
125+
transaction: 'routes/loader-json-response/$id',
126+
},
127+
});
128+
129+
assertSentryEvent(event[2], {
130+
exception: {
131+
values: [
132+
{
133+
type: 'Error',
134+
value: 'Unexpected Server Error from Loader',
135+
stacktrace: expect.any(Object),
136+
mechanism: {
137+
data: {
138+
function: 'loader',
139+
},
140+
handled: true,
141+
type: 'instrument',
142+
},
143+
},
144+
],
145+
},
146+
});
147+
});
89148
});

0 commit comments

Comments
 (0)