diff options
| author | Bobby <[email protected]> | 2026-01-20 14:32:35 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-01-20 14:32:35 +0530 |
| commit | 519a4a81bc3ae4738835a1fcb7cfaf72a06bf13b (patch) | |
| tree | 671623fcda6ff32c671b3dff1dada4be413fc650 | |
| parent | 374fa73e53a3e8613f4b68e8d016c74d557d79a1 (diff) | |
| download | cafe-519a4a81bc3ae4738835a1fcb7cfaf72a06bf13b.tar.xz cafe-519a4a81bc3ae4738835a1fcb7cfaf72a06bf13b.zip | |
Implement custom error handling and templates for HTTP responses
| -rw-r--r-- | controllers/errors.go | 62 | ||||
| -rw-r--r-- | router/router.go | 16 | ||||
| -rw-r--r-- | templates/error.django | 16 |
3 files changed, 93 insertions, 1 deletions
diff --git a/controllers/errors.go b/controllers/errors.go new file mode 100644 index 0000000..ba0e9df --- /dev/null +++ b/controllers/errors.go @@ -0,0 +1,62 @@ +package controllers + +import ( + "cafe/types" + "cafe/utils/shortcuts" + + "github.com/gofiber/fiber/v2" +) + +func BadRequest(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "400 – Bad Request", + ErrorMessage: shortcuts.BuildErrorMessage(err, "The request could not be understood by the server."), + StatusCode: fiber.StatusBadRequest, + }) +} + +func DefaultError(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "Error", + ErrorMessage: shortcuts.BuildErrorMessage(err, "An error occurred while processing your request."), + StatusCode: fiber.StatusInternalServerError, + }) +} + +func Forbidden(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "403 – Forbidden", + ErrorMessage: shortcuts.BuildErrorMessage(err, "You do not have permission to access this resource."), + StatusCode: fiber.StatusForbidden, + }) +} + +func InternalServerError(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "500 – Internal Server Error", + ErrorMessage: shortcuts.BuildErrorMessage(err, "An unexpected error occurred on the server."), + StatusCode: fiber.StatusInternalServerError, + }) +} + +func NotFound(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "404 – Not Found", + ErrorMessage: shortcuts.BuildErrorMessage(err, "The page you are looking for does not exist."), + StatusCode: fiber.StatusNotFound, + }) +} + +func Unauthorized(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "401 – Unauthorized", + ErrorMessage: shortcuts.BuildErrorMessage(err, "You must be logged in to access this resource."), + StatusCode: fiber.StatusUnauthorized, + }) +} diff --git a/router/router.go b/router/router.go index c44d6d3..2428a84 100644 --- a/router/router.go +++ b/router/router.go @@ -1,6 +1,7 @@ package router import ( + "cafe/controllers" "cafe/utils/urls" "github.com/gofiber/fiber/v2" @@ -18,5 +19,18 @@ func ErrorHandler(ctx *fiber.Ctx, err error) error { code = e.Code } - return ctx.Status(code).SendString(err.Error()) + switch code { + case fiber.StatusBadRequest: + return controllers.BadRequest(ctx, err) + case fiber.StatusUnauthorized: + return controllers.Unauthorized(ctx, err) + case fiber.StatusForbidden: + return controllers.Forbidden(ctx, err) + case fiber.StatusNotFound: + return controllers.NotFound(ctx, err) + case fiber.StatusInternalServerError: + return controllers.InternalServerError(ctx, err) + default: + return controllers.DefaultError(ctx, err) + } } diff --git a/templates/error.django b/templates/error.django new file mode 100644 index 0000000..18c86be --- /dev/null +++ b/templates/error.django @@ -0,0 +1,16 @@ +{% extends "layouts/base.django" %} + +{% block title %}{{ ErrorTitle }}{% endblock %} + +{% block content %} + <div class="min-h-screen flex items-center justify-center px-4"> + <div class="max-w-md w-full text-center"> + <h1 class="text-6xl font-bold text-red-500 mb-4">Error</h1> + <h2 class="text-2xl font-semibold mb-4">{{ ErrorTitle }}</h2> + <p class="text-gray-400 mb-8">{{ ErrorMessage }}</p> + <a href="{% url "home" %}" class="inline-block px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition"> + Go Home + </a> + </div> + </div> +{% endblock %}
\ No newline at end of file |
