Files
pulse-signage/src/web/pages/error.js
T
2026-08-03 12:45:27 +01:00

66 lines
2.3 KiB
JavaScript

// Error page renderer for the web UI.
const { renderView } = require('#src/web/view');
const { PERMISSION_DENIED_MESSAGE } = require('#src/rbac');
function getErrorCopy(statusCode, message) {
const normalizedStatusCode = Number(statusCode) || 500;
if (normalizedStatusCode === 403) {
return {
title: 'Access denied',
errorTitle: 'Access denied',
errorMessage: message || PERMISSION_DENIED_MESSAGE,
backLabel: 'Back to dashboard'
};
}
if (normalizedStatusCode === 404) {
return {
title: 'Not found',
errorTitle: 'Oops! Page not found.',
errorMessage: message || 'We could not find the page you were looking for. Meanwhile, you may return to the dashboard or try searching for what you need.',
backLabel: 'Back to dashboard'
};
}
if (normalizedStatusCode >= 500) {
return {
title: 'Something went wrong',
errorTitle: 'Something went wrong.',
errorMessage: message || 'An unexpected error occurred. Please try again in a moment.',
backLabel: 'Back to dashboard'
};
}
return {
title: 'Error',
errorTitle: 'Error',
errorMessage: message || 'An unexpected error occurred.',
backLabel: 'Back to dashboard'
};
}
module.exports = function renderErrorPage(options, currentUser) {
const errorOptions = options || {};
const statusCode = Number(errorOptions.statusCode || 500);
const copy = getErrorCopy(statusCode, String(errorOptions.message || '').trim());
return renderView('error/error', {
title: String(errorOptions.title || copy.title || 'Error').trim(),
active: '',
messageVariant: statusCode === 403 ? 'warning' : 'primary',
currentUser: currentUser || null,
statusCode: statusCode,
errorTitle: String(errorOptions.errorTitle || copy.errorTitle || errorOptions.title || 'Error').trim(),
errorMessage: String(errorOptions.message || copy.errorMessage || 'An unexpected error occurred.').trim(),
detail: String(errorOptions.detail || '').trim(),
backUrl: String(errorOptions.backUrl || '/dashboard').trim() || '/dashboard',
backLabel: String(errorOptions.backLabel || copy.backLabel || 'Back to dashboard').trim() || 'Back to dashboard',
searchUrl: String(errorOptions.searchUrl || '').trim(),
bodyClass: 'error-page bg-dark text-white',
errorShell: true,
stylesheets: [],
scripts: []
});
};