Skip to content

Commit

Permalink
doc: add comments for public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentserpoul committed Apr 7, 2022
1 parent aabef1c commit 74498f1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions body.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
ErrCodeParsingBody = "error_parsing_body"
)

// BindBody will bind the body of the request to the given interface.
func BindBody(r *http.Request, target interface{}) *ErrorResponse {
// nolint: gocritic
// LATER: add more encodings
Expand Down
5 changes: 5 additions & 0 deletions error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/rs/zerolog"
)

// ErrorResponse is a wrapper for the error response body to have a clean way of displaying errors.
type ErrorResponse struct {
Error error `json:"-"`
HTTPStatusCode int `json:"-"`
ErrorCode string `json:"error_code"`
ErrorMsg string `json:"error_msg"`
}

// NewErrorResponse creates a new ErrorResponse.
func NewErrorResponse(
e error,
hsc int,
Expand All @@ -39,6 +41,7 @@ func (her *ErrorResponse) render(log zerolog.Logger, w http.ResponseWriter, r *h
)
}

// IsEqual checks if an error response is equal to another.
func (her *ErrorResponse) IsEqual(e1 *ErrorResponse) bool {
if !errors.Is(e1.Error, her.Error) {
return false
Expand All @@ -59,6 +62,7 @@ func (her *ErrorResponse) IsEqual(e1 *ErrorResponse) bool {
return true
}

// InternalServerError is an error that is returned when an internal server error occurs.
type InternalServerError struct {
Err error
}
Expand All @@ -71,6 +75,7 @@ func (e InternalServerError) ToErrorResponse() *ErrorResponse {
return NewErrorResponse(e, http.StatusInternalServerError, "internal_error", "internal error")
}

// NotFoundError is an error that is returned when a resource is not found.
type NotFoundError struct {
Designation string
}
Expand Down
1 change: 1 addition & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rs/zerolog"
)

// Response is a wrapper for the response body.
type Response struct {
Body any
HTTPStatusCode int
Expand Down
3 changes: 3 additions & 0 deletions url_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"net/http"
)

// NamedURLParamsGetter is the interface that is used to parse the URL parameters.
type NamedURLParamsGetter func(ctx context.Context, key string) (string, *ErrorResponse)

// MissingParamError is the error that is returned when a named URL param is missing.
type MissingParamError struct {
Name string
}
Expand All @@ -25,6 +27,7 @@ func (e MissingParamError) ToErrorResponse() *ErrorResponse {
}
}

// ParsingParamError is the error that is returned when a named URL param is invalid.
type ParsingParamError struct {
Name string
Value string
Expand Down
2 changes: 2 additions & 0 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/rs/zerolog"
)

// TypedHandler is the handler that you are actually handling the response.
type TypedHandler func(r *http.Request) (*Response, *ErrorResponse)

// Wrapper will actually do the boring work of logging an error and render the response.
func Wrapper(
log zerolog.Logger,
f TypedHandler,
Expand Down

0 comments on commit 74498f1

Please sign in to comment.