mirror of
https://github.com/sussy-code/sudo-proxy.git
synced 2026-04-20 08:22:05 +00:00
better error handling
This commit is contained in:
parent
9ff25a4e61
commit
56e84a2a3a
2 changed files with 19 additions and 1 deletions
|
|
@ -6,7 +6,14 @@ export default defineEventHandler(async (event) => {
|
||||||
|
|
||||||
// parse destination URL
|
// parse destination URL
|
||||||
const destination = getQuery<{ destination?: string }>(event).destination;
|
const destination = getQuery<{ destination?: string }>(event).destination;
|
||||||
if (!destination) throw new Error('invalid destination');
|
if (!destination)
|
||||||
|
return sendJson({
|
||||||
|
event,
|
||||||
|
status: 400,
|
||||||
|
data: {
|
||||||
|
error: 'destination query parameter invalid',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// proxy
|
// proxy
|
||||||
await proxyRequest(event, destination, {
|
await proxyRequest(event, destination, {
|
||||||
|
|
|
||||||
11
src/utils/sending.ts
Normal file
11
src/utils/sending.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { H3Event, EventHandlerRequest } from 'h3';
|
||||||
|
|
||||||
|
export function sendJson(ops: {
|
||||||
|
event: H3Event<EventHandlerRequest>;
|
||||||
|
data: Record<string, any>;
|
||||||
|
status?: number;
|
||||||
|
}) {
|
||||||
|
setResponseStatus(ops.event, ops.status ?? 200);
|
||||||
|
appendResponseHeader(ops.event, 'content-type', 'application/json');
|
||||||
|
send(ops.event, JSON.stringify(ops.data, null, 2));
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue