import type { rejectionError } from "./errors"
import type { TokenType } from "@prisma/client"

export default function jwtVerifyCatch(
  tokenType: TokenType,
  err: Error
): rejectionError {
  switch (err.message) {
    case "jwt expired":
      return {
        message: `JWT (${tokenType}) expired!`,
        statusCode: 403,
        solved: true,
        type: "warn",
      }

    case "invalid signature":
      return {
        message: `Invalid JWT (${tokenType}) signature! Token: `,
        statusCode: 401,
        solved: false,
      }

    case "jwt must be provided":
      return {
        message: `No JWT (${tokenType}) given.`,
        statusCode: 401,
        solved: true,
        type: "warn",
      }

    default:
      console.log(err)
      return {
        statusCode: 500,
        message: `Unknown error on 'JWT.verify()'.`,
        solved: false,
      }
  }
}
