fix error handling

This commit is contained in:
Pau 2025-04-15 16:02:00 +02:00
parent 10f7f8c91a
commit 082f7a7002
13 changed files with 45 additions and 89 deletions

View File

@ -30,7 +30,7 @@ const getEndpointsController = (req, res, next, config) => {
) )
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -58,7 +58,7 @@ const getEndpointsByUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -78,7 +78,7 @@ const postEndpointsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -102,16 +102,8 @@ const putEndpointsController = (req, res, next, config) => {
next(result) next(result)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}
if (err.code === 'ER_BAD_NULL_ERROR') {
const error = error404()
return res.status(error.code).json(error)
}
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -130,7 +122,7 @@ const softDeleteEndpointsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -148,7 +140,7 @@ const deleteEndpointsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -52,7 +52,7 @@ const postLoginController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment); const error = errorHandler(err, config.environment);
return res.status(error.code).json(error); res.status(error.code).json(error);
}) })
.finally(() => { .finally(() => {
mysql.end(conn); mysql.end(conn);

View File

@ -29,7 +29,7 @@ const getPermissionController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -78,16 +78,8 @@ const postPermissionController = (req, res, next, config) => {
next(result) next(result)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}
if (err.code === 'ER_BAD_NULL_ERROR') {
const error = error404()
return res.status(error.code).json(error)
}
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -115,7 +107,7 @@ const putPermissionController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -134,7 +126,7 @@ const softDeletePermissionController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -42,7 +42,7 @@ const postRefreshTokenController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -39,12 +39,8 @@ const postRegisterController = (req, res, next, config) => {
next(result) next(result)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = error409();
return sendResponseConflict(res, error);
}
const error = errorHandler(err, config.environment); const error = errorHandler(err, config.environment);
return res.status(err).json(error); res.status(error.code).json(error);
}) })
.finally(() => { .finally(() => {
mysql.end(conn); mysql.end(conn);

View File

@ -44,7 +44,7 @@ const getRoleController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -72,7 +72,7 @@ const getRoleInfoController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -92,16 +92,8 @@ const postRoleController = (req, res, next, config) => {
next(result) next(result)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}
if (err.code === 'ER_BAD_NULL_ERROR') {
const error = error404()
return res.status(error.code).json(error)
}
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -129,7 +121,7 @@ const putRoleController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -148,7 +140,7 @@ const deleteRoleController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -30,7 +30,7 @@ const getRolesHasPermissionsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -57,7 +57,7 @@ const getRolesHasPermissionsByUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -77,7 +77,7 @@ const postRolesHasPermissionsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -102,7 +102,7 @@ const putRolesHasPermissionsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -122,7 +122,7 @@ const softDeleteRolesHasPermissionsController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -34,7 +34,7 @@ const getUserListController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -65,7 +65,7 @@ const getUserInfoController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -96,16 +96,8 @@ const postUserController = (req, res, next, config) => {
next(result) next(result)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}
if (err.code === 'ER_BAD_NULL_ERROR') {
const error = error404()
return res.status(error.code).json(error)
}
const error = errorHandler(err, config.environment)
return res.status(error).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -147,7 +139,7 @@ const putUserController = async (req, res, next, config) => {
} catch (err) { } catch (err) {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
} finally { } finally {
mysql.end(conn) mysql.end(conn)
} }
@ -165,7 +157,7 @@ const softDeleteUserController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -30,7 +30,7 @@ const getCoordinatesListController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -78,16 +78,8 @@ const postCoordinatesController = (req, res, next, config) => {
next(result) next(result)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}
if (err.code === 'ER_BAD_NULL_ERROR') {
const error = error404()
return res.status(error.code).json(error)
}
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -114,7 +106,7 @@ const putCoordinatesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -143,7 +135,7 @@ const deleteCoordinatesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -30,7 +30,7 @@ const getPlaceListController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -164,7 +164,7 @@ const insertPlaceController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -185,7 +185,7 @@ const modifyPlaceController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -206,7 +206,7 @@ const deletePlaceController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -102,7 +102,7 @@ const putReportTypesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment); const error = errorHandler(err, config.environment);
return res.status(error.code).json(error); res.status(error.code).json(error);
}) })
.finally(() => { .finally(() => {
mysql.end(conn); mysql.end(conn);
@ -121,7 +121,7 @@ const softDeleteReportTypesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment); const error = errorHandler(err, config.environment);
return res.status(error.code).json(error); res.status(error.code).json(error);
}) })
.finally(() => { .finally(() => {
mysql.end(conn); mysql.end(conn);

View File

@ -114,7 +114,7 @@ const getUserHasPlacesByPlaceUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -135,7 +135,7 @@ const postUserHasPlacesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -156,7 +156,7 @@ const putUserHasPlacesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -174,7 +174,7 @@ const deleteUserHasPlacesController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -192,7 +192,7 @@ const deleteUserHasPlacesByUserUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -210,7 +210,7 @@ const deleteUserHasPlacesByPlaceUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -84,7 +84,7 @@ const _getRolePermissionsByName = (roleName, config) => {
if (noResults(response)) { if (noResults(response)) {
const err = error404() const err = error404()
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return sendResponseNotFound(res, error) return sendResponseNotFound(response, error)
} }
return response.map(({ permission_action, permission_endpoint }) => ({ return response.map(({ permission_action, permission_endpoint }) => ({
permission_action, permission_action,