Improve errors

This commit is contained in:
aronmal 2023-04-10 22:08:16 +02:00
parent 8193b85274
commit 2a009585f4
Signed by: aronmal
GPG key ID: 816B7707426FC612
10 changed files with 54 additions and 26 deletions

View file

@ -1,6 +1,6 @@
import type { Player } from "@prisma/client" import type { Player } from "@prisma/client"
import bcrypt from "bcrypt" import bcrypt from "bcrypt"
import errors from "../errors" import { rejectionErrors } from "../errors"
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
export default async function checkPasswordIsValid( export default async function checkPasswordIsValid(
@ -11,7 +11,7 @@ export default async function checkPasswordIsValid(
) { ) {
// Validate for correct password // Validate for correct password
const result = await bcrypt.compare(password, player.passwordHash ?? "") const result = await bcrypt.compare(password, player.passwordHash ?? "")
if (!result) return sendError(context, errors.wrongPassword) if (!result) return sendError(context, rejectionErrors.wrongPassword)
return next() return next()
} }

View file

@ -1,5 +1,5 @@
import jwt from "jsonwebtoken" import jwt from "jsonwebtoken"
import errors from "../errors" import { rejectionErrorFns } from "../errors"
import jwtVerifyCatch from "../jwtVerifyCatch" import jwtVerifyCatch from "../jwtVerifyCatch"
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
import type { IdToken, RawToken } from "./createTokenDB" 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) // Making sure the token data is not a string (because it should be an object)
if (typeof data === "string") if (typeof data === "string")
return sendError(context, errors.tokenWasString(type, value)) return sendError(context, rejectionErrorFns.tokenWasString(type, value))
return next({ id: data.id, type }) return next({ id: data.id, type })
} }

View file

@ -1,5 +1,5 @@
import type { Player, Token } from "@prisma/client" import type { Player, Token } from "@prisma/client"
import errors from "../errors" import { rejectionErrors } from "../errors"
import prisma from "../../prisma" import prisma from "../../prisma"
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
@ -15,7 +15,7 @@ export default async function getPlayerByIdDB(
}, },
}) })
if (!player) { if (!player) {
return sendError(context, errors.playerNotFound) return sendError(context, rejectionErrors.playerNotFound)
} }
return next(player) return next(player)

View file

@ -1,5 +1,5 @@
import type { Player } from "@prisma/client" import type { Player } from "@prisma/client"
import errors from "../errors" import { rejectionErrors } from "../errors"
import prisma from "../../prisma" import prisma from "../../prisma"
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
@ -21,7 +21,7 @@ export default async function getPlayerByNameDB(
}, },
}), }),
]).catch(() => null) ]).catch(() => null)
if (player === null) return sendError(context, errors.playerNotFound) if (player === null) return sendError(context, rejectionErrors.playerNotFound)
return next(player) return next(player)
} }

View file

@ -1,7 +1,7 @@
import type { Token } from "@prisma/client" import type { Token } from "@prisma/client"
import type { IdToken } from "./createTokenDB" import type { IdToken } from "./createTokenDB"
import prisma from "../../prisma" import prisma from "../../prisma"
import errors from "../errors" import { rejectionErrorFns, rejectionErrors } from "../errors"
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
async function getTokenDB( async function getTokenDB(
@ -16,9 +16,9 @@ async function getTokenDB(
id, 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({ await prisma.token.update({
where: { where: {

View file

@ -1,6 +1,6 @@
import type { RawToken } from "./createTokenDB" import type { RawToken } from "./createTokenDB"
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
import errors from "../errors" import { rejectionErrorFns } from "../errors"
async function getTokenFromBody( async function getTokenFromBody(
context: API, context: API,
@ -19,7 +19,7 @@ async function getTokenFromBody(
return next({ value, type }) return next({ value, type })
} }
return sendError(context, errors.noToken(type)) return sendError(context, rejectionErrorFns.noToken(type))
} }
export default getTokenFromBody export default getTokenFromBody

View file

@ -1,5 +1,5 @@
import sendError, { API } from "./sendError" import sendError, { API } from "./sendError"
import errors from "../errors" import { rejectionErrors } from "../errors"
async function getUserFromBody( async function getUserFromBody(
context: API, context: API,
@ -12,10 +12,10 @@ async function getUserFromBody(
!("username" in body) || !("username" in body) ||
typeof body.username !== "string" typeof body.username !== "string"
) )
return sendError(context, errors.noUsername) return sendError(context, rejectionErrors.noUsername)
const { username } = body const { username } = body
if (!("password" in body) || typeof body.password !== "string") if (!("password" in body) || typeof body.password !== "string")
return sendError(context, errors.noPassword) return sendError(context, rejectionErrors.noPassword)
const { password } = body const { password } = body
return next(username, password) return next(username, password)

View file

@ -11,6 +11,12 @@ export default function sendError(context: API, err: rejectionError | Error) {
const { res, req } = context const { res, req } = context
// If something went wrong, let the client know with status 500 // If something went wrong, let the client know with status 500
res.status("statusCode" in err ? err.statusCode : 500).end() 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) if ("name" in err) console.log(err)
} }

View file

@ -1,16 +1,21 @@
import { TokenType } from "@prisma/client" import { TokenType } from "@prisma/client"
import { Logging } from "./logging"
export interface rejectionError { export interface rejectionError {
rejected?: boolean rejected?: boolean
message: string message: string
statusCode: number statusCode: number
solved: boolean solved: boolean
type?: Logging
} }
export interface rejectionErrors { interface rejectionErrors {
[key: string]: rejectionError [key: string]: rejectionError
} }
interface rejectionErrorFns {
[key: string]: (...args: any[]) => rejectionError
}
const rejectionErrors = { export const rejectionErrors: rejectionErrors = {
noCookie: { noCookie: {
rejected: true, rejected: true,
message: "Unauthorized. No cookie.", message: "Unauthorized. No cookie.",
@ -22,29 +27,51 @@ const rejectionErrors = {
message: "Unauthorized. No Body.", message: "Unauthorized. No Body.",
statusCode: 401, statusCode: 401,
solved: true, solved: true,
type: "warn",
}, },
noUsername: { noUsername: {
rejected: true, rejected: true,
message: "No username in request body!", message: "No username in request body!",
statusCode: 401, statusCode: 401,
solved: true, solved: true,
type: "warn",
}, },
noPassword: { noPassword: {
rejected: true, rejected: true,
message: "No password in request body!", message: "No password in request body!",
statusCode: 401, statusCode: 401,
solved: true, solved: true,
type: "warn",
}, },
wrongPassword: { wrongPassword: {
message: "Passwords do not match!", message: "Passwords do not match!",
statusCode: 401, statusCode: 401,
solved: true, solved: true,
type: "warn",
}, },
playerNotFound: { playerNotFound: {
message: "Player name not found in DB!", message: "Player name not found in DB!",
statusCode: 401, statusCode: 401,
solved: false, 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) { tokenWasString(tokenType: TokenType, tokenValue: string) {
return { return {
rejected: true, rejected: true,
@ -62,12 +89,6 @@ const rejectionErrors = {
type: "warn", type: "warn",
} }
}, },
tokenUsed: {
rejected: true,
message: "DBToken was already used!",
statusCode: 401,
solved: true,
},
noToken(tokenType: TokenType) { noToken(tokenType: TokenType) {
return { return {
rejected: true, rejected: true,
@ -77,4 +98,3 @@ const rejectionErrors = {
} }
}, },
} }
export default rejectionErrors

View file

@ -11,6 +11,7 @@ export default function jwtVerifyCatch(
message: `JWT (${tokenType}) expired!`, message: `JWT (${tokenType}) expired!`,
statusCode: 403, statusCode: 403,
solved: true, solved: true,
type: "warn",
} }
case "invalid signature": case "invalid signature":
@ -25,6 +26,7 @@ export default function jwtVerifyCatch(
message: `No JWT (${tokenType}) given.`, message: `No JWT (${tokenType}) given.`,
statusCode: 401, statusCode: 401,
solved: true, solved: true,
type: "warn",
} }
default: default: