Skip to content

Commit 4bd95b7

Browse files
committed
Add thrown 500 redirects.
1 parent 7e197cc commit 4bd95b7

File tree

4 files changed

+130
-26
lines changed

4 files changed

+130
-26
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: 55 additions & 9 deletions
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,15 +56,57 @@ describe('Remix API Actions', () => {
5256
values: [
5357
{
5458
type: 'Error',
55-
value: 'Error',
56-
stacktrace: expect.any(Object),
57-
mechanism: {
58-
data: {
59-
function: 'action',
60-
},
61-
handled: true,
62-
type: 'instrument',
63-
},
59+
value: 'Unexpected Server Error',
60+
},
61+
],
62+
},
63+
});
64+
});
65+
66+
it('handles a thrown 500 response', async () => {
67+
const baseURL = await runServer();
68+
const url = `${baseURL}/action-json-response/-2`;
69+
70+
const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest(url, 3, 'post');
71+
72+
assertSentryTransaction(transaction_1[2], {
73+
contexts: {
74+
trace: {
75+
op: 'http.server',
76+
status: 'ok',
77+
tags: {
78+
method: 'POST',
79+
'http.status_code': '302',
80+
},
81+
},
82+
},
83+
tags: {
84+
transaction: 'routes/action-json-response/$id',
85+
},
86+
});
87+
88+
assertSentryTransaction(transaction_2[2], {
89+
contexts: {
90+
trace: {
91+
op: 'http.server',
92+
status: 'internal_error',
93+
tags: {
94+
method: 'GET',
95+
'http.status_code': '500',
96+
},
97+
},
98+
},
99+
tags: {
100+
transaction: 'routes/action-json-response/$id',
101+
},
102+
});
103+
104+
assertSentryEvent(event[2], {
105+
exception: {
106+
values: [
107+
{
108+
type: 'Error',
109+
value: 'Unexpected Server Error from Loader',
64110
},
65111
],
66112
},

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

Lines changed: 52 additions & 10 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,15 +48,7 @@ describe('Remix API Loaders', () => {
4848
values: [
4949
{
5050
type: 'Error',
51-
value: 'Error',
52-
stacktrace: expect.any(Object),
53-
mechanism: {
54-
data: {
55-
function: 'loader',
56-
},
57-
handled: true,
58-
type: 'instrument',
59-
},
51+
value: 'Unexpected Server Error from Loader',
6052
},
6153
],
6254
},
@@ -86,4 +78,54 @@ describe('Remix API Loaders', () => {
8678
],
8779
});
8880
});
81+
82+
it('handles a thrown 500 response', async () => {
83+
const baseURL = await runServer();
84+
const url = `${baseURL}/loader-json-response/-1`;
85+
86+
const [transaction_1, event, transaction_2] = await getMultipleEnvelopeRequest(url, 3);
87+
88+
assertSentryTransaction(transaction_1[2], {
89+
contexts: {
90+
trace: {
91+
op: 'http.server',
92+
status: 'ok',
93+
tags: {
94+
method: 'GET',
95+
'http.status_code': '302',
96+
},
97+
},
98+
},
99+
tags: {
100+
transaction: 'routes/loader-json-response/$id',
101+
},
102+
});
103+
104+
assertSentryTransaction(transaction_2[2], {
105+
contexts: {
106+
trace: {
107+
op: 'http.server',
108+
status: 'internal_error',
109+
tags: {
110+
method: 'GET',
111+
'http.status_code': '500',
112+
},
113+
},
114+
},
115+
tags: {
116+
transaction: 'routes/loader-json-response/$id',
117+
},
118+
});
119+
120+
assertSentryEvent(event[2], {
121+
exception: {
122+
values: [
123+
{
124+
type: 'Error',
125+
value: 'Unexpected Server Error from Loader',
126+
},
127+
],
128+
},
129+
});
130+
});
89131
});

0 commit comments

Comments
 (0)