-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
respond_with_error.js
97 lines (88 loc) · 1.89 KB
/
respond_with_error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { render_response } from './render.js';
import { load_node } from './load_node.js';
import { coalesce_to_error } from '../../../utils/error.js';
/**
* @typedef {import('./types.js').Loaded} Loaded
* @typedef {import('types').SSROptions} SSROptions
* @typedef {import('types').SSRState} SSRState
*/
/**
* @param {{
* event: import('types').RequestEvent;
* options: SSROptions;
* state: SSRState;
* $session: any;
* status: number;
* error: Error;
* resolve_opts: import('types').RequiredResolveOptions;
* }} opts
*/
export async function respond_with_error({
event,
options,
state,
$session,
status,
error,
resolve_opts
}) {
try {
const branch = [];
let stuff = {};
if (resolve_opts.ssr) {
const default_layout = await options.manifest._.nodes[0](); // 0 is always the root layout
const default_error = await options.manifest._.nodes[1](); // 1 is always the root error
const layout_loaded = /** @type {Loaded} */ (
await load_node({
event,
options,
state,
route: null,
node: default_layout,
$session,
stuff: {},
is_error: false,
is_leaf: false
})
);
const error_loaded = /** @type {Loaded} */ (
await load_node({
event,
options,
state,
route: null,
node: default_error,
$session,
stuff: layout_loaded ? layout_loaded.stuff : {},
is_error: true,
is_leaf: false,
status,
error
})
);
branch.push(layout_loaded, error_loaded);
stuff = error_loaded.stuff;
}
return await render_response({
options,
state,
$session,
page_config: {
hydrate: options.hydrate,
router: options.router
},
stuff,
status,
error,
branch,
event,
resolve_opts
});
} catch (err) {
const error = coalesce_to_error(err);
options.handle_error(error, event);
return new Response(error.stack, {
status: 500
});
}
}