Add error handling

This commit is contained in:
KMKoushik
2024-04-22 14:10:34 +10:00
parent 493e9fb63a
commit 66732d2345
6 changed files with 165 additions and 5 deletions

View File

@@ -1,15 +1,22 @@
import { Context } from "hono";
import { hashToken } from "../auth";
import { db } from "../db";
import { UnsendApiError } from "./api-error";
export const getTeamFromToken = async (c: Context) => {
const authHeader = c.req.header("Authorization");
if (!authHeader) {
throw new Error("No Authorization header provided");
throw new UnsendApiError({
code: "UNAUTHORIZED",
message: "No Authorization header provided",
});
}
const token = authHeader.split(" ")[1]; // Assuming the Authorization header is in the format "Bearer <token>"
if (!token) {
throw new Error("No bearer token provided");
throw new UnsendApiError({
code: "UNAUTHORIZED",
message: "No Authorization header provided",
});
}
const hashedToken = hashToken(token);
@@ -25,7 +32,10 @@ export const getTeamFromToken = async (c: Context) => {
});
if (!team) {
throw new Error("No team found for this token");
throw new UnsendApiError({
code: "FORBIDDEN",
message: "Invalid API token",
});
}
return team;