-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added PUT and PATCH routes (#38)
- Added PUT and PATCH routes - Improved request error handling - Reorganized code Closes #5
- Loading branch information
1 parent
7b584c9
commit 9d91c40
Showing
19 changed files
with
401 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package errors | ||
|
||
type BadRequest struct { | ||
Message string | ||
} | ||
|
||
func (e *BadRequest) Error() string { | ||
return e.Message | ||
} | ||
|
||
type NotFound struct { | ||
Message string | ||
} | ||
|
||
func (e *NotFound) Error() string { | ||
return e.Message | ||
} | ||
|
||
type ServiceUnavailable struct { | ||
Message string | ||
} | ||
|
||
func (e *ServiceUnavailable) Error() string { | ||
return e.Message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package middlewares | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/go-chi/render" | ||
|
||
serviceErrors "todo-service/internal/errors" | ||
"todo-service/internal/storage" | ||
"todo-service/internal/types" | ||
) | ||
|
||
type ErrorHandler struct{} | ||
|
||
func (e *ErrorHandler) Wrap(handler func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var notFoundError *serviceErrors.NotFound | ||
var badRequestError *serviceErrors.BadRequest | ||
var serviceUnavailable *serviceErrors.ServiceUnavailable | ||
|
||
err := handler(w, r) | ||
|
||
if (errors.As(err, ¬FoundError)) || (errors.Is(err, storage.ErrNotFound)) { | ||
render.Status(r, http.StatusNotFound) | ||
response := types.ErrorResponse{ | ||
Status: http.StatusText(http.StatusNotFound), | ||
Error: err.Error(), | ||
} | ||
render.JSON(w, r, response) | ||
return | ||
} | ||
|
||
if errors.As(err, &badRequestError) { | ||
render.Status(r, http.StatusBadRequest) | ||
response := types.ErrorResponse{ | ||
Status: http.StatusText(http.StatusBadRequest), | ||
Error: err.Error(), | ||
} | ||
render.JSON(w, r, response) | ||
return | ||
} | ||
|
||
if errors.As(err, &serviceUnavailable) { | ||
render.Status(r, http.StatusServiceUnavailable) | ||
response := types.ErrorResponse{ | ||
Status: http.StatusText(http.StatusServiceUnavailable), | ||
Error: err.Error(), | ||
} | ||
render.JSON(w, r, response) | ||
return | ||
} | ||
|
||
if err != nil { | ||
render.Status(r, http.StatusInternalServerError) | ||
response := types.ErrorResponse{ | ||
Status: http.StatusText(http.StatusInternalServerError), | ||
Error: "Encountered an unexpected server error: " + err.Error(), | ||
} | ||
render.JSON(w, r, response) | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package types | ||
|
||
// @openapi | ||
// components: | ||
// | ||
// responses: | ||
// NotFound: | ||
// description: The specified resource was not found | ||
// content: | ||
// application/json: | ||
// schema: | ||
// $ref: '#/components/schemas/Error' | ||
// Unauthorized: | ||
// description: Unauthorized | ||
// content: | ||
// application/json: | ||
// schema: | ||
// $ref: '#/components/schemas/Error' | ||
// schemas: | ||
// Error: | ||
// type: object | ||
// properties: | ||
// status: | ||
// type: string | ||
// error: | ||
// type: string | ||
type ErrorResponse struct { | ||
Status string `json:"status"` | ||
Error string `json:"error"` | ||
} | ||
|
||
// @openapi | ||
// components: | ||
// | ||
// schemas: | ||
// PatchBody: | ||
// type: object | ||
// description: A JSONPatch document as defined by RFC 6902 | ||
// additionalProperties: false | ||
// required: | ||
// - op | ||
// - path | ||
// properties: | ||
// op: | ||
// type: string | ||
// description: The operation to be performed | ||
// enum: | ||
// - add | ||
// - remove | ||
// - replace | ||
// - move | ||
// - copy | ||
// - test | ||
// path: | ||
// type: string | ||
// description: A JSON-Pointer | ||
// value: | ||
// description: The value to be used within the operations. | ||
// from: | ||
// type: string | ||
// description: A string containing a JSON Pointer value. | ||
type PatchBody struct{} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.