github.com/paulwerner/bookkeeper@v0.1.0/router/handler/errors.go (about)

     1  package handler
     2  
     3  import (
     4  	"net/http"
     5  
     6  	d "github.com/paulwerner/bookkeeper/pkg/domain"
     7  )
     8  
     9  type errorResponse struct {
    10  	Errors map[string]any `json:"errors"`
    11  }
    12  
    13  func newErrorResponse(err any) (errorResponse, int) {
    14  	e := errorResponse{}
    15  	e.Errors = make(map[string]any)
    16  
    17  	switch serr := err.(type) {
    18  	case d.NotFoundError:
    19  		e.Errors["msg"] = serr.Error()
    20  		return e, http.StatusNotFound
    21  
    22  	case d.InvalidEntityError:
    23  		e.Errors["msg"] = serr.Error()
    24  		return e, http.StatusUnprocessableEntity
    25  
    26  	case d.InvalidPasswordError:
    27  		e.Errors["msg"] = serr.Error()
    28  		return e, http.StatusForbidden
    29  
    30  	case d.InvalidLengthError:
    31  		e.Errors["msg"] = serr.Error()
    32  		return e, http.StatusUnprocessableEntity
    33  
    34  	case d.AlreadyInUseError:
    35  		e.Errors["msg"] = serr.Error()
    36  		return e, http.StatusConflict
    37  
    38  	case d.InvalidAccessTokenError:
    39  		e.Errors["msg"] = serr.Error()
    40  		return e, http.StatusForbidden
    41  
    42  	default:
    43  		e.Errors["msg"] = "unexpected error occurred"
    44  		return e, http.StatusInternalServerError
    45  	}
    46  }