ApiErrorMessage.jsx 2.3 KB

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