mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Improved error handling for useSession middleware.
This commit is contained in:
parent
8666965930
commit
c5bffb97cc
@ -20,14 +20,19 @@ export const useCors = createMiddleware(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const useSession = createMiddleware(async (req, res, next) => {
|
export const useSession = createMiddleware(async (req, res, next) => {
|
||||||
const session = await findSession(req as NextApiRequestCollect);
|
try {
|
||||||
|
const session = await findSession(req as NextApiRequestCollect);
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
log('useSession: Session not found');
|
log('useSession: Session not found');
|
||||||
return badRequest(res, 'Session not found.');
|
return badRequest(res, 'Session not found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
(req as any).session = session;
|
||||||
|
} catch (e: any) {
|
||||||
|
return badRequest(res, e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
(req as any).session = session;
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||||||
const { payload } = getJsonBody<CollectRequestBody>(req);
|
const { payload } = getJsonBody<CollectRequestBody>(req);
|
||||||
|
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
return null;
|
throw new Error('Invalid payload.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if cache token is passed
|
// Check if cache token is passed
|
||||||
@ -29,14 +29,14 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||||||
const { website: websiteId, hostname, screen, language } = payload;
|
const { website: websiteId, hostname, screen, language } = payload;
|
||||||
|
|
||||||
if (!validate(websiteId)) {
|
if (!validate(websiteId)) {
|
||||||
return null;
|
throw new Error('Invalid website ID.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find website
|
// Find website
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
|
|
||||||
if (!website) {
|
if (!website) {
|
||||||
throw new Error(`Website not found: ${websiteId}`);
|
throw new Error(`Website not found: ${websiteId}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
|
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user