[ABANDONED] React/Redux front end for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. // errors.ts
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import { History } from 'history'
  14. import { setFieldNotification } from '../actions/forms'
  15. import { showNotification } from '../actions/notifications'
  16. import { AppThunkDispatch, FormNotification, NotificationType } from '../types'
  17. export function handleApiError(err: HttpError, dispatch: AppThunkDispatch, history?: History) {
  18. console.error('Error:', err)
  19. if (err instanceof ServerError) {
  20. dispatch(showNotification(NotificationType.Error, 'Server Error'))
  21. return
  22. }
  23. if (err instanceof BadRequestError) {
  24. dispatch(showNotification(NotificationType.Error, `Error: ${err.message}`))
  25. for (const error of err.errors) {
  26. const { field, type, message } = error
  27. if (field) dispatch(setFieldNotification(field, type, message))
  28. }
  29. return
  30. }
  31. if (err instanceof UnauthorizedError) {
  32. dispatch(showNotification(NotificationType.Error, 'You need to be logged in.'))
  33. if (history) history.push('/login')
  34. return
  35. }
  36. if (err instanceof NotFoundError) {
  37. dispatch(showNotification(NotificationType.Error, 'Not found.'))
  38. return
  39. }
  40. dispatch(showNotification(NotificationType.Error, `Error: ${err.message}`))
  41. }
  42. export class HttpError extends Error {
  43. statusCode: number
  44. errors: FormNotification[]
  45. constructor(message = 'Unknown Error') {
  46. super(message)
  47. this.name = 'HttpError'
  48. this.statusCode = 500
  49. this.errors = []
  50. }
  51. }
  52. export class ServerError extends HttpError {
  53. constructor() {
  54. super('Server Error')
  55. this.name = 'ServerError'
  56. this.statusCode = 500
  57. }
  58. }
  59. export class BadRequestError extends HttpError {
  60. constructor(message = 'Bad Request', errors: FormNotification[] = []) {
  61. super(message)
  62. this.name = 'BadRequestError'
  63. this.statusCode = 400
  64. this.errors = errors
  65. }
  66. }
  67. export class UnauthorizedError extends HttpError {
  68. constructor(message = 'Unauthorized') {
  69. super(message)
  70. this.name = 'UnauthorizedError'
  71. this.statusCode = 401
  72. }
  73. }
  74. export class ForbiddenError extends HttpError {
  75. constructor(message = 'Forbidden') {
  76. super(message)
  77. this.name = 'ForbiddenError'
  78. this.statusCode = 403
  79. }
  80. }
  81. export class NotFoundError extends HttpError {
  82. constructor(message = 'Not Found') {
  83. super(message)
  84. this.name = 'NotFoundError'
  85. this.statusCode = 404
  86. }
  87. }