diff --git a/internal/app/handlers/engagement_handler.go b/internal/app/handlers/engagement_handler.go index 9e35182..1c75b7c 100644 --- a/internal/app/handlers/engagement_handler.go +++ b/internal/app/handlers/engagement_handler.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" domain "github.com/haydenhargreaves/Potion/internal/domain/server" + "github.com/haydenhargreaves/Potion/internal/templates/components" ) func EngagementViewRecipe(ctx *gin.Context) { @@ -101,6 +102,7 @@ func EngagementFavoriteRecipe(ctx *gin.Context) { recipeId, _ := strconv.Atoi(id) if _, err := deps.EngagementService.UserFavoriteRecipe(user.Id, recipeId); err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Something went wrong. %s.", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, "message": err.Error(), diff --git a/internal/app/handlers/page_handler.go b/internal/app/handlers/page_handler.go index a29b705..8c84971 100755 --- a/internal/app/handlers/page_handler.go +++ b/internal/app/handlers/page_handler.go @@ -11,6 +11,7 @@ import ( domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe" domain "github.com/haydenhargreaves/Potion/internal/domain/server" domainServer "github.com/haydenhargreaves/Potion/internal/domain/server" + "github.com/haydenhargreaves/Potion/internal/templates/components" layouts "github.com/haydenhargreaves/Potion/internal/templates/layouts" pages "github.com/haydenhargreaves/Potion/internal/templates/pages" templates "github.com/haydenhargreaves/Potion/internal/templates/pages" @@ -41,6 +42,7 @@ func HomePage(ctx *gin.Context) { userId := ctx.MustGet("userId").(int) madeRecipes, err := deps.RecipeService.GetUserMadeRecipes(userId, 6) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting made recipes. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, "message": fmt.Sprintf("Error getting made recipes. %s\n", err.Error()), @@ -49,9 +51,10 @@ func HomePage(ctx *gin.Context) { } viewedRecipes, err := deps.RecipeService.GetUserViewedRecipes(userId, 6) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting viewed recipes. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, - "message": fmt.Sprintf("Error getting made recipes. %s\n", err.Error()), + "message": fmt.Sprintf("Error getting viewed recipes. %s\n", err.Error()), }) return } @@ -59,9 +62,10 @@ func HomePage(ctx *gin.Context) { // Get the recipe of the week recipeOfTheWeek, err := deps.RecipeService.GetRecipeOfTheWeek(&userId) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting recipe of the week. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, - "message": fmt.Sprintf("Error getting made recipes. %s\n", err.Error()), + "message": fmt.Sprintf("Error getting recipe of the week. %s\n", err.Error()), }) return } @@ -82,9 +86,10 @@ func HomePage(ctx *gin.Context) { // Get the recipe of the week recipeOfTheWeek, err := deps.RecipeService.GetRecipeOfTheWeek(nil) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting recipe of the week. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, - "message": fmt.Sprintf("Error getting made recipes. %s\n", err.Error()), + "message": fmt.Sprintf("Error getting recipe of the week. %s\n", err.Error()), }) return } @@ -178,6 +183,7 @@ func ProfilePage(ctx *gin.Context) { recipes, err := deps.RecipeService.GetUserRecipes(user.Id) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting recipes. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, "message": fmt.Sprintf("Error getting recipes. %s\n", err.Error()), @@ -187,9 +193,10 @@ func ProfilePage(ctx *gin.Context) { favorites, err := deps.RecipeService.GetUserFavoriteRecipes(user.Id) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting favorite recipes. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, - "message": fmt.Sprintf("Error getting recipes. %s\n", err.Error()), + "message": fmt.Sprintf("Error getting favorite recipes. %s\n", err.Error()), }) return } @@ -197,6 +204,7 @@ func ProfilePage(ctx *gin.Context) { // Get the engagement data, not sure what will happen when errors occur engagements, err := deps.EngagementService.GetUserEngagement(user.Id, 6) if err != nil { + components.RenderErrorBanner(ctx, fmt.Sprintf("Error getting user engagements. %s\n", err.Error())) ctx.JSON(http.StatusInternalServerError, gin.H{ "status": http.StatusInternalServerError, "message": fmt.Sprintf("Error getting user engagements. %s\n", err.Error()), @@ -217,7 +225,6 @@ func ListPage(ctx *gin.Context) { ctx.HTML(http.StatusOK, "", layouts.AppLayout(title, page)) } -// TODO: Figure out how to handle errors, think we just need a simple display. func RecipePage(ctx *gin.Context) { // Call recipe service to get via ID deps := ctx.MustGet("deps").(*domainServer.InjectedDependencies) @@ -226,7 +233,7 @@ func RecipePage(ctx *gin.Context) { // Parse ID parsed, err := strconv.Atoi(id) if err != nil { - fmt.Printf("ERROR: %s\n", err.Error()) + components.RenderErrorBanner(ctx, fmt.Sprintf("ERROR: %s", err.Error())) ctx.JSON(400, err.Error()) return } @@ -251,7 +258,7 @@ func RecipePage(ctx *gin.Context) { // Get recipe recipe, err := deps.RecipeService.GetRecipe(parsed, userId) if err != nil { - fmt.Printf("ERROR: %s\n", err.Error()) + components.RenderErrorBanner(ctx, fmt.Sprintf("ERROR: %s", err.Error())) ctx.JSON(400, err.Error()) return } @@ -259,7 +266,7 @@ func RecipePage(ctx *gin.Context) { // Get user (owner) user, err := deps.UserService.GetUser(recipe.UserId) if err != nil { - fmt.Printf("ERROR: %s\n", err.Error()) + components.RenderErrorBanner(ctx, fmt.Sprintf("ERROR: %s", err.Error())) ctx.JSON(400, err.Error()) return } diff --git a/internal/templates/components/error.templ b/internal/templates/components/error.templ new file mode 100644 index 0000000..7c75b50 --- /dev/null +++ b/internal/templates/components/error.templ @@ -0,0 +1,58 @@ +package components + +import "github.com/gin-gonic/gin" + +templ errorIcon() { + +} + +templ closeButton() { + +} + +templ errorBanner(message string) { +
+ { message } +
+") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(message) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/components/error.templ`, Line: 46, Col: 13} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "