Files
sablier/internal/api/problemdetail.go
Alexis Couvreur 00cc153d7a fix: add http error responses (#494)
This features adds rfc7807 Problem detail responses when an error happens processing a request.

This will greatly improve the common issues  with "blank pages" and "404 pages" issues which should now properly tell the user what input was wrong (group that does not exist, container name that does not exist, etc.)
2025-02-02 00:00:49 -05:00

53 lines
1.6 KiB
Go

package api
import (
"github.com/sablierapp/sablier/app/sessions"
"github.com/sablierapp/sablier/app/theme"
"github.com/tniswong/go.rfcx/rfc7807"
"net/http"
)
func ProblemError(e error) rfc7807.Problem {
return rfc7807.Problem{
Type: "https://sablierapp.dev/#/errors?id=internal-error",
Title: http.StatusText(http.StatusInternalServerError),
Status: http.StatusInternalServerError,
Detail: e.Error(),
}
}
func ProblemValidation(e error) rfc7807.Problem {
return rfc7807.Problem{
Type: "https://sablierapp.dev/#/errors?id=validation-error",
Title: "Validation Failed",
Status: http.StatusBadRequest,
Detail: e.Error(),
}
}
func ProblemGroupNotFound(e sessions.ErrGroupNotFound) rfc7807.Problem {
pb := rfc7807.Problem{
Type: "https://sablierapp.dev/#/errors?id=group-not-found",
Title: "Group not found",
Status: http.StatusNotFound,
Detail: "The group you requested does not exist. It is possible that the group has not been scanned yet.",
}
_ = pb.Extend("availableGroups", e.AvailableGroups)
_ = pb.Extend("requestGroup", e.Group)
_ = pb.Extend("error", e.Error())
return pb
}
func ProblemThemeNotFound(e theme.ErrThemeNotFound) rfc7807.Problem {
pb := rfc7807.Problem{
Type: "https://sablierapp.dev/#/errors?id=theme-not-found",
Title: "Theme not found",
Status: http.StatusNotFound,
Detail: "The theme you requested does not exist among the default themes and the custom themes (if any).",
}
_ = pb.Extend("availableTheme", e.AvailableThemes)
_ = pb.Extend("requestTheme", e.Theme)
_ = pb.Extend("error", e.Error())
return pb
}