use errorStatus & move error structs to responses.go
This commit is contained in:
3 files changed
+16
-25
No files matched your search
+5
-13
@@ -3,7 +3,6 @@ package handlers
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -79,22 +78,17 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// validate the request body
|
||||
if err := json.NewDecoder(r.Body).Decode(&cowDetails); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
response := models.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"error": err.Error()}}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
// use the validator library to validate required fields
|
||||
if validationErr := validate.Struct(&cowDetails); validationErr != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
response := models.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"error": validationErr.Error()}}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(cowDetails)
|
||||
|
||||
newCow := models.Cow{
|
||||
ID: primitive.NewObjectID().Hex(),
|
||||
Details: cowDetails,
|
||||
@@ -102,9 +96,7 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
result, err := c.DB.InsertOne(ctx, newCow)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response := models.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"error": err.Error()}}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
config.ErrorStatus("failed to insert cow", http.StatusBadRequest, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -126,7 +118,7 @@ func (c Cow) UpdateCowHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
dbResp, err := c.DB.UpdateOne(context.Background(), bson.M{"_id": cowID}, update)
|
||||
if err != nil {
|
||||
config.ErrorStatus("failed to update cow by ID", http.StatusNotFound, w, err)
|
||||
config.ErrorStatus("the cow could not be updated", http.StatusNotFound, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package models
|
||||
|
||||
// ErrorMessageResponse returns the error message response struct
|
||||
type ErrorMessageResponse struct {
|
||||
Response MessageError
|
||||
}
|
||||
|
||||
// MessageError contains the inner details for the error message response
|
||||
type MessageError struct {
|
||||
Message string
|
||||
Error string
|
||||
}
|
||||
@@ -11,3 +11,14 @@ type UserResponse struct {
|
||||
Message string `json:"message"`
|
||||
Data map[string]interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// ErrorMessageResponse returns the error message response struct
|
||||
type ErrorMessageResponse struct {
|
||||
Response MessageError
|
||||
}
|
||||
|
||||
// MessageError contains the inner details for the error message response
|
||||
type MessageError struct {
|
||||
Message string
|
||||
Error string
|
||||
}
|
||||
Reference in new issue
Block a user