|
import { DEV } from 'esm-env'; |
|
import { json, text } from '../../exports/index.js'; |
|
import { coalesce_to_error, get_message, get_status } from '../../utils/error.js'; |
|
import { negotiate } from '../../utils/http.js'; |
|
import { HttpError } from '../control.js'; |
|
import { fix_stack_trace } from '../shared-server.js'; |
|
import { ENDPOINT_METHODS } from '../../constants.js'; |
|
|
|
|
|
export function is_pojo(body) { |
|
if (typeof body !== 'object') return false; |
|
|
|
if (body) { |
|
if (body instanceof Uint8Array) return false; |
|
if (body instanceof ReadableStream) return false; |
|
} |
|
|
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function method_not_allowed(mod, method) { |
|
return text(`${method} method not allowed`, { |
|
status: 405, |
|
headers: { |
|
|
|
|
|
allow: allowed_methods(mod).join(', ') |
|
} |
|
}); |
|
} |
|
|
|
|
|
export function allowed_methods(mod) { |
|
const allowed = ENDPOINT_METHODS.filter((method) => method in mod); |
|
|
|
if ('GET' in mod || 'HEAD' in mod) allowed.push('HEAD'); |
|
|
|
return allowed; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function static_error_page(options, status, message) { |
|
let page = options.templates.error({ status, message }); |
|
|
|
if (DEV) { |
|
|
|
page = page.replace('</head>', '<script type="module" src="/@vite/client"></script></head>'); |
|
} |
|
|
|
return text(page, { |
|
headers: { 'content-type': 'text/html; charset=utf-8' }, |
|
status |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function handle_fatal_error(event, options, error) { |
|
error = error instanceof HttpError ? error : coalesce_to_error(error); |
|
const status = get_status(error); |
|
const body = await handle_error_and_jsonify(event, options, error); |
|
|
|
|
|
const type = negotiate(event.request.headers.get('accept') || 'text/html', [ |
|
'application/json', |
|
'text/html' |
|
]); |
|
|
|
if (event.isDataRequest || type === 'application/json') { |
|
return json(body, { |
|
status |
|
}); |
|
} |
|
|
|
return static_error_page(options, status, body.message); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function handle_error_and_jsonify(event, options, error) { |
|
if (error instanceof HttpError) { |
|
return error.body; |
|
} |
|
|
|
if (__SVELTEKIT_DEV__ && typeof error == 'object') { |
|
fix_stack_trace(error); |
|
} |
|
|
|
const status = get_status(error); |
|
const message = get_message(error); |
|
|
|
return (await options.hooks.handleError({ error, event, status, message })) ?? { message }; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function redirect_response(status, location) { |
|
const response = new Response(undefined, { |
|
status, |
|
headers: { location } |
|
}); |
|
return response; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function clarify_devalue_error(event, error) { |
|
if (error.path) { |
|
return `Data returned from \`load\` while rendering ${event.route.id} is not serializable: ${error.message} (data${error.path})`; |
|
} |
|
|
|
if (error.path === '') { |
|
return `Data returned from \`load\` while rendering ${event.route.id} is not a plain object`; |
|
} |
|
|
|
|
|
return error.message; |
|
} |
|
|
|
|
|
|
|
|
|
export function stringify_uses(node) { |
|
const uses = []; |
|
|
|
if (node.uses && node.uses.dependencies.size > 0) { |
|
uses.push(`"dependencies":${JSON.stringify(Array.from(node.uses.dependencies))}`); |
|
} |
|
|
|
if (node.uses && node.uses.search_params.size > 0) { |
|
uses.push(`"search_params":${JSON.stringify(Array.from(node.uses.search_params))}`); |
|
} |
|
|
|
if (node.uses && node.uses.params.size > 0) { |
|
uses.push(`"params":${JSON.stringify(Array.from(node.uses.params))}`); |
|
} |
|
|
|
if (node.uses?.parent) uses.push('"parent":1'); |
|
if (node.uses?.route) uses.push('"route":1'); |
|
if (node.uses?.url) uses.push('"url":1'); |
|
|
|
return `"uses":{${uses.join(',')}}`; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function warn_with_callsite(message, offset = 0) { |
|
if (DEV) { |
|
const stack = fix_stack_trace(new Error()).split('\n'); |
|
const line = stack.at(3 + offset); |
|
message += `\n${line}`; |
|
} |
|
|
|
console.warn(message); |
|
} |
|
|