[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.

97 lines
2.4 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. import { History } from 'history'
  2. import { setFieldNotification } from '../actions/forms'
  3. import { showNotification } from '../actions/notifications'
  4. import { AppThunkDispatch, FormNotification, NotificationType } from '../types'
  5. export function handleApiError(err: HttpError, dispatch: AppThunkDispatch, history?: History) {
  6. console.error('Error:', err)
  7. if (err instanceof ServerError) {
  8. dispatch(showNotification(NotificationType.Error, 'Server Error'))
  9. return
  10. }
  11. if (err instanceof BadRequestError) {
  12. dispatch(showNotification(NotificationType.Error, `Error: ${err.message}`))
  13. for (const error of err.errors) {
  14. const { field, type, message } = error
  15. if (field) dispatch(setFieldNotification(field, type, message))
  16. }
  17. return
  18. }
  19. if (err instanceof UnauthorizedError) {
  20. dispatch(showNotification(NotificationType.Error, 'You need to be logged in.'))
  21. if (history) history.push('/login')
  22. return
  23. }
  24. if (err instanceof NotFoundError) {
  25. dispatch(showNotification(NotificationType.Error, 'Not found.'))
  26. return
  27. }
  28. dispatch(showNotification(NotificationType.Error, `Error: ${err.message}`))
  29. }
  30. export class HttpError extends Error {
  31. statusCode: number
  32. errors: FormNotification[]
  33. constructor(message = 'Unknown Error') {
  34. super(message)
  35. this.name = 'HttpError'
  36. this.statusCode = 500
  37. this.errors = []
  38. }
  39. }
  40. export class ServerError extends HttpError {
  41. constructor() {
  42. super('Server Error')
  43. this.name = 'ServerError'
  44. this.statusCode = 500
  45. }
  46. }
  47. export class BadRequestError extends HttpError {
  48. constructor(message = 'Bad Request', errors: FormNotification[] = []) {
  49. super(message)
  50. this.name = 'BadRequestError'
  51. this.statusCode = 400
  52. this.errors = errors
  53. }
  54. }
  55. export class UnauthorizedError extends HttpError {
  56. constructor(message = 'Unauthorized') {
  57. super(message)
  58. this.name = 'UnauthorizedError'
  59. this.statusCode = 401
  60. }
  61. }
  62. export class ForbiddenError extends HttpError {
  63. constructor(message = 'Forbidden') {
  64. super(message)
  65. this.name = 'ForbiddenError'
  66. this.statusCode = 403
  67. }
  68. }
  69. export class NotFoundError extends HttpError {
  70. constructor(message = 'Not Found') {
  71. super(message)
  72. this.name = 'NotFoundError'
  73. this.statusCode = 404
  74. }
  75. }