From 344b6bd2c974b4cadc5a53b6d94c360c3971caf1 Mon Sep 17 00:00:00 2001 From: Bernd Storath <999999bst@gmail.com> Date: Mon, 19 Aug 2024 13:30:50 +0200 Subject: [PATCH] improve zod errors, consistent server errors --- package.json | 2 +- src/package.json | 2 +- src/server/utils/WireGuard.ts | 18 +++++------- src/server/utils/types.ts | 55 ++++++++++++++++++++++------------- 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 5d8e09cf..68823c12 100644 --- a/package.json +++ b/package.json @@ -7,5 +7,5 @@ "sudostart": "sudo docker run --env WG_HOST=0.0.0.0 --name wg-easy --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl=\"net.ipv4.conf.all.src_valid_mark=1\" --mount type=bind,source=\"$(pwd)\"/config,target=/etc/wireguard -p 51820:51820/udp -p 51821:51821/tcp wg-easy", "start": "docker run --env WG_HOST=0.0.0.0 --name wg-easy --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl=\"net.ipv4.conf.all.src_valid_mark=1\" --mount type=bind,source=\"$(pwd)\"/config,target=/etc/wireguard -p 51820:51820/udp -p 51821:51821/tcp wg-easy" }, - "packageManager": "pnpm@9.7.0" + "packageManager": "pnpm@9.7.1" } diff --git a/src/package.json b/src/package.json index 5aa9f083..40d9d777 100644 --- a/src/package.json +++ b/src/package.json @@ -47,5 +47,5 @@ "typescript": "^5.5.4", "vue-tsc": "^2.0.29" }, - "packageManager": "pnpm@9.7.0" + "packageManager": "pnpm@9.7.1" } diff --git a/src/server/utils/WireGuard.ts b/src/server/utils/WireGuard.ts index 072e63c7..ff79a3a9 100644 --- a/src/server/utils/WireGuard.ts +++ b/src/server/utils/WireGuard.ts @@ -6,14 +6,6 @@ import QRCode from 'qrcode'; const debug = debug_logger('WireGuard'); -class ServerError extends Error { - statusCode: number; - constructor(message: string, statusCode = 500) { - super(message); - this.statusCode = statusCode; - } -} - type Server = { privateKey: string; publicKey: string; @@ -216,7 +208,10 @@ ${ const config = await this.getConfig(); const client = config.clients[clientId]; if (!client) { - throw new ServerError(`Client Not Found: ${clientId}`, 404); + throw createError({ + statusCode: 404, + statusMessage: `Client Not Found: ${clientId}`, + }); } return client; @@ -356,7 +351,10 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`; const client = await this.getClient({ clientId }); if (!isValidIPv4(address)) { - throw new ServerError(`Invalid Address: ${address}`, 400); + throw createError({ + statusCode: 400, + statusMessage: `Invalid Address: ${address}`, + }); } client.address = address; diff --git a/src/server/utils/types.ts b/src/server/utils/types.ts index 254a5ba7..7820bee7 100644 --- a/src/server/utils/types.ts +++ b/src/server/utils/types.ts @@ -11,44 +11,59 @@ const safeStringRefine = z const id = z .string() .uuid('Client ID must be a valid UUID') - .and(safeStringRefine); + .pipe(safeStringRefine); const address = z .string({ message: 'Address must be a valid string' }) - .and(safeStringRefine); + .pipe(safeStringRefine); const name = z .string({ message: 'Name must be a valid string' }) .min(1, 'Name must be at least 1 Character') - .and(safeStringRefine); + .pipe(safeStringRefine); const file = z .string({ message: 'File must be a valid string' }) - .and(safeStringRefine); + .pipe(safeStringRefine); const password = z .string({ message: 'Password must be a valid string' }) - .and(safeStringRefine); + .pipe(safeStringRefine); -export const clientIdType = z.object({ - clientId: id, -}); +export const clientIdType = z.object( + { + clientId: id, + }, + { message: "This shouldn't happen" } +); -export const addressType = z.object({ - address: address, -}); +export const addressType = z.object( + { + address: address, + }, + { message: 'Body must be a valid object' } +); -export const nameType = z.object({ - name: name, -}); +export const nameType = z.object( + { + name: name, + }, + { message: 'Body must be a valid object' } +); -export const fileType = z.object({ - file: file, -}); +export const fileType = z.object( + { + file: file, + }, + { message: 'Body must be a valid object' } +); -export const passwordType = z.object({ - password: password, -}); +export const passwordType = z.object( + { + password: password, + }, + { message: 'Body must be a valid object' } +); export function validateZod(schema: ZodSchema) { return async (data: unknown) => {