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 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()
}

View file

@ -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 })
}

View file

@ -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)

View file

@ -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)
}

View file

@ -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: {

View file

@ -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

View file

@ -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)

View file

@ -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)
}

View file

@ -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

View file

@ -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: