Skip to content

Commit

Permalink
chore: added more logging for some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pnicolli committed May 27, 2024
1 parent ac3ae90 commit 73a6d13
Show file tree
Hide file tree
Showing 2 changed files with 416 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/customizations/volto/components/theme/Error/Error.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @module components/theme/Error/Error
* Customization:
* - added logging of errors
*/

import React from 'react';
import loadable from '@loadable/component';
import config from '@plone/volto/registry';

const sentryLibraries = {
Sentry: loadable.lib(
() => import(/* webpackChunkName: "s_entry-browser" */ '@sentry/browser'),
),
};

/**
* Error function.
* @function Error
* @returns {string} Markup of the error page.
*/
const Error = (props) => {
const { views } = config;
const { error } = props;
let FoundView;

// CUSTOMIZATION: added logging of errors
const notifySentry = (error) => {
const loaders = Object.entries(sentryLibraries).map(
([name, Lib]) =>
new Promise((resolve) =>
Lib.load().then((mod) => resolve([name, mod])),
),
);
Promise.all(loaders).then((libs) => {
const libraries = Object.assign(
{},
...libs.map(([name, lib]) => ({ [name]: lib })),
);
libraries.Sentry.captureException(error);
});
};

if (error.status === undefined) {
// For some reason, while development and if CORS is in place and the
// requested resource is 404, it returns undefined as status, then the
// next statement will fail
// eslint-disable-next-line no-console
console.error(
'DEV MODE CORS ERROR in Error component: ',
JSON.stringify(props, null, 2),
);
notifySentry(props);
FoundView = views.errorViews.corsError;
} else {
if (error.status.toString() === 'corsError') {
// eslint-disable-next-line no-console
console.error(
'CORS ERROR in Error component: ',
JSON.stringify(props, null, 2),
);
notifySentry(props);
}
FoundView = views.errorViews[error.status.toString()];
}
if (!FoundView) {
FoundView = views.errorViews['404']; // default to 404
}
return (
<div id="view">
<FoundView {...props} />
</div>
);
};

export default Error;
Loading

0 comments on commit 73a6d13

Please sign in to comment.