From 2a009585f455db0af3d7328741379cb05bcaf121 Mon Sep 17 00:00:00 2001 From: aronmal Date: Mon, 10 Apr 2023 22:08:16 +0200 Subject: [PATCH] Improve errors --- .../components/checkPasswordIsValid.ts | 4 +- .../backend/components/checkTokenIsValid.ts | 4 +- .../lib/backend/components/getPlayerByIdDB.ts | 4 +- .../backend/components/getPlayerByNameDB.ts | 4 +- .../lib/backend/components/getTokenDB.ts | 6 +-- .../backend/components/getTokenFromBody.ts | 4 +- .../lib/backend/components/getUserFromBody.ts | 6 +-- .../lib/backend/components/sendError.ts | 8 +++- leaky-ships/lib/backend/errors.ts | 38 ++++++++++++++----- leaky-ships/lib/backend/jwtVerifyCatch.ts | 2 + 10 files changed, 54 insertions(+), 26 deletions(-) diff --git a/leaky-ships/lib/backend/components/checkPasswordIsValid.ts b/leaky-ships/lib/backend/components/checkPasswordIsValid.ts index 19c08b3..f0da82b 100644 --- a/leaky-ships/lib/backend/components/checkPasswordIsValid.ts +++ b/leaky-ships/lib/backend/components/checkPasswordIsValid.ts @@ -1,6 +1,6 @@ import type { Player } from "@prisma/client" import bcrypt from "bcrypt" -import errors from "../errors" +import { rejectionErrors } from "../errors" import sendError, { API } from "./sendError" export default async function checkPasswordIsValid( @@ -11,7 +11,7 @@ export default async function checkPasswordIsValid( ) { // Validate for correct password const result = await bcrypt.compare(password, player.passwordHash ?? "") - if (!result) return sendError(context, errors.wrongPassword) + if (!result) return sendError(context, rejectionErrors.wrongPassword) return next() } diff --git a/leaky-ships/lib/backend/components/checkTokenIsValid.ts b/leaky-ships/lib/backend/components/checkTokenIsValid.ts index c2d6829..8589f07 100644 --- a/leaky-ships/lib/backend/components/checkTokenIsValid.ts +++ b/leaky-ships/lib/backend/components/checkTokenIsValid.ts @@ -1,5 +1,5 @@ import jwt from "jsonwebtoken" -import errors from "../errors" +import { rejectionErrorFns } from "../errors" import jwtVerifyCatch from "../jwtVerifyCatch" import sendError, { API } from "./sendError" import type { IdToken, RawToken } from "./createTokenDB" @@ -21,7 +21,7 @@ async function checkTokenIsValid( } // Making sure the token data is not a string (because it should be an object) if (typeof data === "string") - return sendError(context, errors.tokenWasString(type, value)) + return sendError(context, rejectionErrorFns.tokenWasString(type, value)) return next({ id: data.id, type }) } diff --git a/leaky-ships/lib/backend/components/getPlayerByIdDB.ts b/leaky-ships/lib/backend/components/getPlayerByIdDB.ts index 2461575..b7b9137 100644 --- a/leaky-ships/lib/backend/components/getPlayerByIdDB.ts +++ b/leaky-ships/lib/backend/components/getPlayerByIdDB.ts @@ -1,5 +1,5 @@ import type { Player, Token } from "@prisma/client" -import errors from "../errors" +import { rejectionErrors } from "../errors" import prisma from "../../prisma" import sendError, { API } from "./sendError" @@ -15,7 +15,7 @@ export default async function getPlayerByIdDB( }, }) if (!player) { - return sendError(context, errors.playerNotFound) + return sendError(context, rejectionErrors.playerNotFound) } return next(player) diff --git a/leaky-ships/lib/backend/components/getPlayerByNameDB.ts b/leaky-ships/lib/backend/components/getPlayerByNameDB.ts index d57b041..9ae50b4 100644 --- a/leaky-ships/lib/backend/components/getPlayerByNameDB.ts +++ b/leaky-ships/lib/backend/components/getPlayerByNameDB.ts @@ -1,5 +1,5 @@ import type { Player } from "@prisma/client" -import errors from "../errors" +import { rejectionErrors } from "../errors" import prisma from "../../prisma" import sendError, { API } from "./sendError" @@ -21,7 +21,7 @@ export default async function getPlayerByNameDB( }, }), ]).catch(() => null) - if (player === null) return sendError(context, errors.playerNotFound) + if (player === null) return sendError(context, rejectionErrors.playerNotFound) return next(player) } diff --git a/leaky-ships/lib/backend/components/getTokenDB.ts b/leaky-ships/lib/backend/components/getTokenDB.ts index 5140eca..5cd176a 100644 --- a/leaky-ships/lib/backend/components/getTokenDB.ts +++ b/leaky-ships/lib/backend/components/getTokenDB.ts @@ -1,7 +1,7 @@ import type { Token } from "@prisma/client" import type { IdToken } from "./createTokenDB" import prisma from "../../prisma" -import errors from "../errors" +import { rejectionErrorFns, rejectionErrors } from "../errors" import sendError, { API } from "./sendError" async function getTokenDB( @@ -16,9 +16,9 @@ async function getTokenDB( id, }, }) - if (!tokenDB) return sendError(context, errors.tokenNotFound(type)) + if (!tokenDB) return sendError(context, rejectionErrorFns.tokenNotFound(type)) - if (tokenDB.used) return sendError(context, errors.tokenUsed) + if (tokenDB.used) return sendError(context, rejectionErrors.tokenUsed) await prisma.token.update({ where: { diff --git a/leaky-ships/lib/backend/components/getTokenFromBody.ts b/leaky-ships/lib/backend/components/getTokenFromBody.ts index 1fdc98f..65f1ca9 100644 --- a/leaky-ships/lib/backend/components/getTokenFromBody.ts +++ b/leaky-ships/lib/backend/components/getTokenFromBody.ts @@ -1,6 +1,6 @@ import type { RawToken } from "./createTokenDB" import sendError, { API } from "./sendError" -import errors from "../errors" +import { rejectionErrorFns } from "../errors" async function getTokenFromBody( context: API, @@ -19,7 +19,7 @@ async function getTokenFromBody( return next({ value, type }) } - return sendError(context, errors.noToken(type)) + return sendError(context, rejectionErrorFns.noToken(type)) } export default getTokenFromBody diff --git a/leaky-ships/lib/backend/components/getUserFromBody.ts b/leaky-ships/lib/backend/components/getUserFromBody.ts index b994081..dc2057f 100644 --- a/leaky-ships/lib/backend/components/getUserFromBody.ts +++ b/leaky-ships/lib/backend/components/getUserFromBody.ts @@ -1,5 +1,5 @@ import sendError, { API } from "./sendError" -import errors from "../errors" +import { rejectionErrors } from "../errors" async function getUserFromBody( context: API, @@ -12,10 +12,10 @@ async function getUserFromBody( !("username" in body) || typeof body.username !== "string" ) - return sendError(context, errors.noUsername) + return sendError(context, rejectionErrors.noUsername) const { username } = body if (!("password" in body) || typeof body.password !== "string") - return sendError(context, errors.noPassword) + return sendError(context, rejectionErrors.noPassword) const { password } = body return next(username, password) diff --git a/leaky-ships/lib/backend/components/sendError.ts b/leaky-ships/lib/backend/components/sendError.ts index 1ba67c3..587495c 100644 --- a/leaky-ships/lib/backend/components/sendError.ts +++ b/leaky-ships/lib/backend/components/sendError.ts @@ -11,6 +11,12 @@ export default function sendError(context: API, err: rejectionError | Error) { const { res, req } = context // If something went wrong, let the client know with status 500 res.status("statusCode" in err ? err.statusCode : 500).end() - logging(err.message, ["solved" in err && err.solved ? "debug" : "error"], req) + logging( + err.message, + "type" in err && err.type + ? [err.type] + : ["solved" in err && err.solved ? "debug" : "error"], + req + ) if ("name" in err) console.log(err) } diff --git a/leaky-ships/lib/backend/errors.ts b/leaky-ships/lib/backend/errors.ts index 662f530..3e7422a 100644 --- a/leaky-ships/lib/backend/errors.ts +++ b/leaky-ships/lib/backend/errors.ts @@ -1,16 +1,21 @@ import { TokenType } from "@prisma/client" +import { Logging } from "./logging" export interface rejectionError { rejected?: boolean message: string statusCode: number solved: boolean + type?: Logging } -export interface rejectionErrors { +interface rejectionErrors { [key: string]: rejectionError } +interface rejectionErrorFns { + [key: string]: (...args: any[]) => rejectionError +} -const rejectionErrors = { +export const rejectionErrors: rejectionErrors = { noCookie: { rejected: true, message: "Unauthorized. No cookie.", @@ -22,29 +27,51 @@ const rejectionErrors = { message: "Unauthorized. No Body.", statusCode: 401, solved: true, + type: "warn", }, noUsername: { rejected: true, message: "No username in request body!", statusCode: 401, solved: true, + type: "warn", }, noPassword: { rejected: true, message: "No password in request body!", statusCode: 401, solved: true, + type: "warn", }, wrongPassword: { message: "Passwords do not match!", statusCode: 401, solved: true, + type: "warn", }, playerNotFound: { message: "Player name not found in DB!", statusCode: 401, solved: false, + type: "warn", }, + tokenUsed: { + rejected: true, + message: "DBToken was already used!", + statusCode: 401, + solved: true, + type: "warn", + }, + registered: { + rejected: true, + message: "Player is already registered!", + statusCode: 403, + solved: true, + type: "warn", + }, +} + +export const rejectionErrorFns: rejectionErrorFns = { tokenWasString(tokenType: TokenType, tokenValue: string) { return { rejected: true, @@ -62,12 +89,6 @@ const rejectionErrors = { type: "warn", } }, - tokenUsed: { - rejected: true, - message: "DBToken was already used!", - statusCode: 401, - solved: true, - }, noToken(tokenType: TokenType) { return { rejected: true, @@ -77,4 +98,3 @@ const rejectionErrors = { } }, } -export default rejectionErrors diff --git a/leaky-ships/lib/backend/jwtVerifyCatch.ts b/leaky-ships/lib/backend/jwtVerifyCatch.ts index eaac65e..8f2e96e 100644 --- a/leaky-ships/lib/backend/jwtVerifyCatch.ts +++ b/leaky-ships/lib/backend/jwtVerifyCatch.ts @@ -11,6 +11,7 @@ export default function jwtVerifyCatch( message: `JWT (${tokenType}) expired!`, statusCode: 403, solved: true, + type: "warn", } case "invalid signature": @@ -25,6 +26,7 @@ export default function jwtVerifyCatch( message: `No JWT (${tokenType}) given.`, statusCode: 401, solved: true, + type: "warn", } default: