Improve centralized error handling

This commit is contained in:
richardtekula
2025-12-04 07:39:52 +01:00
parent 109cae1167
commit 35dfa07668
14 changed files with 266 additions and 336 deletions

View File

@@ -1,4 +1,5 @@
import jwt from 'jsonwebtoken';
import { AuthenticationError } from './errors.js';
/**
* Generuje access JWT token
@@ -33,10 +34,10 @@ export const verifyAccessToken = (token) => {
return jwt.verify(token, process.env.JWT_SECRET);
} catch (error) {
if (error.name === 'TokenExpiredError') {
throw new Error('Token expiroval');
throw new AuthenticationError('Token expiroval');
}
if (error.name === 'JsonWebTokenError') {
throw new Error('Neplatný token');
throw new AuthenticationError('Neplatný token');
}
throw error;
}
@@ -53,10 +54,10 @@ export const verifyRefreshToken = (token) => {
return jwt.verify(token, process.env.JWT_REFRESH_SECRET);
} catch (error) {
if (error.name === 'TokenExpiredError') {
throw new Error('Refresh token expiroval');
throw new AuthenticationError('Refresh token expiroval');
}
if (error.name === 'JsonWebTokenError') {
throw new Error('Neplatný refresh token');
throw new AuthenticationError('Neplatný refresh token');
}
throw error;
}