ApiErrorMessage.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. const ApiErrorMessage = (props) => {
  5. const {
  6. t, errors, targetPath,
  7. } = props;
  8. function reload() {
  9. window.location.reload();
  10. }
  11. if (errors == null) {
  12. return null;
  13. }
  14. function renderMessage(err) {
  15. function renderMessageByErrorCode() {
  16. switch (err.code) {
  17. case 'already_exists':
  18. return (
  19. <>
  20. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.already_exists') }</strong>
  21. <small><a href={targetPath}>{targetPath} <i className="icon-login"></i></a></small>
  22. </>
  23. );
  24. case 'notfound_or_forbidden':
  25. return (
  26. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.notfound_or_forbidden') }</strong>
  27. );
  28. case 'user_not_admin':
  29. return (
  30. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.user_not_admin') }</strong>
  31. );
  32. case 'outdated':
  33. return (
  34. <>
  35. <strong><i className="icon-fw icon-bulb"></i> { t('page_api_error.outdated') }</strong>
  36. <a className="btn-link" onClick={reload}>
  37. <i className="fa fa-angle-double-right"></i> { t('Load latest') }
  38. </a>
  39. </>
  40. );
  41. case 'invalid_path':
  42. return (
  43. <strong><i className="icon-fw icon-ban"></i> Invalid path</strong>
  44. );
  45. default:
  46. return (
  47. <strong><i className="icon-fw icon-ban"></i> Unknown error occured</strong>
  48. );
  49. }
  50. }
  51. if (err.code != null) {
  52. return renderMessageByErrorCode();
  53. }
  54. if (err.message != null) {
  55. return err.message;
  56. }
  57. }
  58. return (
  59. <>
  60. {errors.map((error) => {
  61. return (
  62. <span key={error.message} className="text-danger">
  63. {renderMessage(error)}
  64. </span>
  65. );
  66. })}
  67. </>
  68. );
  69. };
  70. ApiErrorMessage.propTypes = {
  71. t: PropTypes.func.isRequired, // i18next
  72. errors: PropTypes.array,
  73. targetPath: PropTypes.string,
  74. };
  75. export default withTranslation()(ApiErrorMessage);