Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
grulex committed Oct 28, 2023
1 parent 39ce383 commit 82f99bb
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 16 deletions.
4 changes: 2 additions & 2 deletions http/httputil/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ResponseWrapper(f HttpUseCase) http.HandlerFunc {

func responseError(handleError *HandleError, w http.ResponseWriter) {
if handleError.Type == ErrorInternal {
log.Println("Handler Error: ", string(handleError.JsonEncode()), "err", handleError.Err)
log.Printf("Handler Error: %+v\n", handleError)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
Expand All @@ -42,7 +42,7 @@ func responseError(handleError *HandleError, w http.ResponseWriter) {
}{handleError}
responseJson, err := json.Marshal(errorResp)
if err != nil {
log.Println("can't encode json", err, "error", handleError.Err.Error())
log.Printf("Handler Error: %+v\n json: %+v\n", err, errorResp)
http.Error(w, "can't encode json response error", handleError.GetHttpStatus())
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func MakeAddProductToWishlistUsecase(
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting product",
Err: err,
},
}
}
Expand All @@ -128,6 +129,7 @@ func MakeAddProductToWishlistUsecase(
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error creating product",
Err: err,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func MakeBookWishlistItemUsecase(wService wishlistService) httputil.HttpUseCase
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist item",
Err: err,
},
}
}
Expand All @@ -90,6 +91,7 @@ func MakeBookWishlistItemUsecase(wService wishlistService) httputil.HttpUseCase
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error booking wishlist item",
Err: err,
},
}
}
Expand All @@ -105,6 +107,7 @@ func isValidWishlist(r *http.Request, wService wishlistService, wishlistID strin
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}, false
}
Expand Down
3 changes: 3 additions & 0 deletions http/usecase/wishlists/get_wishlist/get_wishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func MakeGetWishlistUsecase(sService subscribeService, wService wishlistService,
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}
}
Expand All @@ -78,6 +79,7 @@ func MakeGetWishlistUsecase(sService subscribeService, wService wishlistService,
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting subscribe",
Err: err,
},
}
}
Expand All @@ -91,6 +93,7 @@ func MakeGetWishlistUsecase(sService subscribeService, wService wishlistService,
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting avatar",
Err: err,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func MakeGetWishlistItemsUsecase(wService wishlistService, productService produc
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist items",
Err: err,
},
}
}
Expand All @@ -83,6 +84,7 @@ func MakeGetWishlistItemsUsecase(wService wishlistService, productService produc
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting products",
Err: err,
},
}
}
Expand All @@ -102,6 +104,7 @@ func MakeGetWishlistItemsUsecase(wService wishlistService, productService produc
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting images",
Err: err,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func MakeRemoveProductFromWishlistUsecase(wService wishlistService) httputil.Htt
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error removing item from wishlist",
Err: err,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func MakeSubscribeWishlistUsecase(wService wishlistService, sService subscribeSe
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error subscribing to wishlist",
Err: err,
},
}
}
Expand All @@ -70,6 +71,7 @@ func isValidWishlist(r *http.Request, wService wishlistService, wishlistID strin
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}, false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func MakeUnBookWishlistItemUsecase(wService wishlistService) httputil.HttpUseCas
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist item",
Err: err,
},
}
}
Expand All @@ -80,6 +81,7 @@ func MakeUnBookWishlistItemUsecase(wService wishlistService) httputil.HttpUseCas
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}
}
Expand All @@ -101,6 +103,7 @@ func MakeUnBookWishlistItemUsecase(wService wishlistService) httputil.HttpUseCas
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error unBooking wishlist item",
Err: err,
},
}
}
Expand All @@ -116,6 +119,7 @@ func isValidWishlist(r *http.Request, wService wishlistService, wishlistID strin
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}, false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func MakeUnSubscribeWishlistUsecase(wService wishlistService, sService subscribe
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error subscribing to wishlist",
Err: err,
},
}
}
Expand All @@ -70,6 +71,7 @@ func isValidWishlist(r *http.Request, wService wishlistService, wishlistID strin
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}, false
}
Expand Down
1 change: 1 addition & 0 deletions http/usecase/wishlists/update_wishlist/update_wishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func MakeUpdateWishlistUsecase(wService wishlistService, fService fileService, i
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}
}
Expand Down
43 changes: 29 additions & 14 deletions http/usecase/wishlists/upload_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/base64"
"fmt"
"github.com/corona10/goimagehash"
"github.com/grulex/go-wishlist/http/httputil"
filePkg "github.com/grulex/go-wishlist/pkg/file"
Expand Down Expand Up @@ -33,29 +32,45 @@ func UploadBase64Image(ctx context.Context, fService fileService, iService image
},
}
}
invalidJpegResult := httputil.HandleResult{
Error: &httputil.HandleError{
Type: httputil.ErrorBadData,
Message: "invalid jpeg image",
Err: err,
},
}
imageObject, format, err := image.Decode(bytes.NewReader(decodedSrc))
imageObject, _, err := image.Decode(bytes.NewReader(decodedSrc))
if err != nil {
fmt.Println(format)
return nil, invalidJpegResult
return nil, httputil.HandleResult{
Error: &httputil.HandleError{
Type: httputil.ErrorBadData,
Message: "invalid jpeg image",
Err: err,
},
}
}
aHash, err := goimagehash.AverageHash(imageObject)
if err != nil {
return nil, invalidJpegResult
return nil, httputil.HandleResult{
Error: &httputil.HandleError{
Type: httputil.ErrorBadData,
Message: "invalid jpeg image",
Err: err,
},
}
}
pHash, err := goimagehash.PerceptionHash(imageObject)
if err != nil {
return nil, invalidJpegResult
return nil, httputil.HandleResult{
Error: &httputil.HandleError{
Type: httputil.ErrorBadData,
Message: "invalid jpeg image",
Err: err,
},
}
}
dHash, err := goimagehash.DifferenceHash(imageObject)
if err != nil {
return nil, invalidJpegResult
return nil, httputil.HandleResult{
Error: &httputil.HandleError{
Type: httputil.ErrorBadData,
Message: "invalid jpeg image",
Err: err,
},
}
}

avatarLink, err := fService.UploadPhoto(ctx, bytes.NewReader(decodedSrc))
Expand Down
1 change: 1 addition & 0 deletions http/usecase/wishlists/wishlist_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func IsValidWishlistAccess(ctx context.Context, wService wishlistService, wishli
Error: &httputil.HandleError{
Type: httputil.ErrorInternal,
Message: "Error getting wishlist",
Err: err,
},
}, false
}
Expand Down

0 comments on commit 82f99bb

Please sign in to comment.