gravitiq

Untitled

Nov 21st, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. soID, err := ctr.Service.CreateServiceOutcome(document)
  2. if err != nil {
  3. elogerrors.LogErr(err, ctr.Logger)
  4.  
  5. var dbErrors []DbError
  6.  
  7. switch {
  8. case errors.Is(elogerrors.Cause(err), apiservice.ErrValidation):
  9. w.WriteHeader(http.StatusBadRequest)
  10.  
  11. dbErrors = []DbError{{
  12. ErrId: 1,
  13. ErrMsg: string(elogerrors.PrintErr(err)),
  14. }}
  15.  
  16. case errors.Is(elogerrors.Cause(err), apiservice.ErrServiceOutcomeAlreadyExists):
  17. w.WriteHeader(http.StatusBadRequest)
  18.  
  19. dbErrors = []DbError{{
  20. ErrId: 2,
  21. ErrMsg: string(elogerrors.PrintErr(err)),
  22. }}
  23.  
  24. default:
  25. var serviceOutcomeErrors apiservice.ServiceOutcomeErrors
  26.  
  27. ok := errors.As(elogerrors.Cause(err), &serviceOutcomeErrors)
  28. if ok {
  29. w.WriteHeader(http.StatusBadRequest)
  30.  
  31. dbErrors = make([]DbError, 0, len(serviceOutcomeErrors))
  32.  
  33. for _, outcomeError := range serviceOutcomeErrors {
  34. dbError := DbError{
  35. ErrId: outcomeError.ErrId,
  36. ErrMsg: outcomeError.ErrMsg,
  37. }
  38.  
  39. dbErrors = append(dbErrors, dbError)
  40. }
  41.  
  42. break
  43. }
  44.  
  45. w.WriteHeader(http.StatusInternalServerError)
  46.  
  47. dbErrors = []DbError{{
  48. ErrId: 0,
  49. ErrMsg: string(elogerrors.PrintErr(err)),
  50. }}
  51.  
  52. }
  53.  
  54. dto := createErrorsDTO{dbErrors}
  55.  
  56. err = json.NewEncoder(w).Encode(dto)
  57. if err != nil {
  58. elogerrors.LogErr(err, ctr.Logger)
  59.  
  60. return
  61. }
  62.  
  63. return
  64. }
Add Comment
Please, Sign In to add comment