Improve errors
This commit is contained in:
parent
8193b85274
commit
2a009585f4
10 changed files with 54 additions and 26 deletions
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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 })
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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: {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue