mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-07 20:20:58 -06:00
[server] Add json body to 500s and descriptions to all errors
This commit is contained in:
@@ -10,19 +10,19 @@ import (
|
||||
type ApiErr struct {
|
||||
Status int `json:"status"`
|
||||
Code string `json:"code"`
|
||||
Details string `json:"details"`
|
||||
Message string `json:"msg"`
|
||||
}
|
||||
|
||||
func New(httpStatus int, code, details string) error {
|
||||
func New(httpStatus int, code, msg string) error {
|
||||
return &ApiErr{
|
||||
Status: httpStatus,
|
||||
Code: code,
|
||||
Details: details,
|
||||
Message: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ApiErr) Error() string {
|
||||
return fmt.Sprintf("%d %s (%s)", e.Status, e.Code, e.Details)
|
||||
return fmt.Sprintf("%d %s (%s)", e.Status, e.Code, e.Message)
|
||||
}
|
||||
|
||||
func Is(err, target error) bool {
|
||||
|
||||
@@ -22,16 +22,16 @@ func createLoginRouteHandler(a *core.App) func(c *gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
username, ok := c.GetQuery("username")
|
||||
if !ok {
|
||||
panic(errors.New(http.StatusBadRequest, "missing_username", ""))
|
||||
panic(errors.New(http.StatusBadRequest, "missing_username", "Username Not Specified"))
|
||||
}
|
||||
password, ok := c.GetQuery("password")
|
||||
if !ok {
|
||||
panic(errors.New(http.StatusBadRequest, "missing_password", ""))
|
||||
panic(errors.New(http.StatusBadRequest, "missing_password", "Password Not Specified"))
|
||||
}
|
||||
|
||||
if user, err := a.VerifyUserPassword(c.Request.Context(), username, password); err != nil {
|
||||
if errors.Is(err, core.ErrCredentialsInvalid) {
|
||||
panic(errors.New(http.StatusUnauthorized, "credentials_invalid", ""))
|
||||
panic(errors.New(http.StatusUnauthorized, "credentials_invalid", "Invalid Credentials"))
|
||||
}
|
||||
panic(err)
|
||||
} else {
|
||||
|
||||
@@ -2,7 +2,6 @@ package routes
|
||||
|
||||
import (
|
||||
iofs "io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/shroff/phylum/server/internal/api/auth"
|
||||
@@ -25,7 +24,7 @@ func handleMobileHomeRoute(c *gin.Context) {
|
||||
r, err := fs.ResourceByID(user.Home())
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errors.New(http.StatusNotFound, errCodeResourceNotFound, "")
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -12,9 +12,11 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core"
|
||||
)
|
||||
|
||||
const errCodeResourceNotFound = "resource_not_found"
|
||||
const errCodeResourceIdInvalid = "resource_id_invalid"
|
||||
const errCodeResourceNotCollection = "resource_not_collection"
|
||||
var (
|
||||
errResourceIdInvalid = errors.New(http.StatusBadRequest, "resource_id_invalid", "Invalid UUID")
|
||||
errResourceNotFound = errors.New(http.StatusNotFound, "resource_not_found", "Resource Not Found")
|
||||
errResourceNotDirectory = errors.New(http.StatusMethodNotAllowed, "resource_not_directory", "Resource Not Directory")
|
||||
)
|
||||
|
||||
type resourceResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
@@ -44,14 +46,14 @@ func SetupResourceRoutes(r *gin.RouterGroup) {
|
||||
func handleResourceMetadataRoute(c *gin.Context) {
|
||||
resourceId, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
panic(errors.New(http.StatusBadRequest, errCodeResourceIdInvalid, ""))
|
||||
panic(errResourceIdInvalid)
|
||||
}
|
||||
|
||||
fs := auth.GetFileSystem(c)
|
||||
resource, err := fs.ResourceByID(resourceId)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errors.New(http.StatusNotFound, errCodeResourceNotFound, "")
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
@@ -62,20 +64,20 @@ func handleResourceMetadataRoute(c *gin.Context) {
|
||||
func handleResourceLsRoute(c *gin.Context) {
|
||||
resourceId, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
panic(errors.New(http.StatusBadRequest, errCodeResourceIdInvalid, ""))
|
||||
panic(errResourceIdInvalid)
|
||||
}
|
||||
|
||||
fs := auth.GetFileSystem(c)
|
||||
resource, err := fs.ResourceByID(resourceId)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errors.New(http.StatusNotFound, errCodeResourceNotFound, "")
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if !resource.IsDir() {
|
||||
panic(errors.New(http.StatusBadRequest, errCodeResourceNotCollection, ""))
|
||||
panic(errResourceNotDirectory)
|
||||
}
|
||||
children, err := fs.ReadDir(resource)
|
||||
if err != nil {
|
||||
@@ -91,14 +93,14 @@ func handleResourceLsRoute(c *gin.Context) {
|
||||
func handleResourceDetailsRoute(c *gin.Context) {
|
||||
resourceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
panic(errors.New(http.StatusBadRequest, errCodeResourceIdInvalid, ""))
|
||||
panic(errResourceIdInvalid)
|
||||
}
|
||||
|
||||
fs := auth.GetFileSystem(c)
|
||||
r, err := fs.ResourceByID(resourceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errors.New(http.StatusNotFound, errCodeResourceNotFound, "")
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package appcmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/fvbock/endless"
|
||||
@@ -63,7 +64,13 @@ func createEngine(logBody bool, corsEnabled bool, corsOrigins []string) *gin.Eng
|
||||
if !core.Default.Debug {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.Default()
|
||||
engine := gin.New()
|
||||
engine.Use(gin.Logger(), gin.CustomRecovery(func(c *gin.Context, err any) {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"code": "internal_server_error",
|
||||
"msg": "Internal Server Error",
|
||||
})
|
||||
}))
|
||||
if core.Default.Debug && logBody {
|
||||
engine.Use(logBodyMiddleware)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user