feat: scaffold codespace backend
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"codespace/internal/util"
|
||||
"codespace/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// writeError maps a service-layer error to an HTTP JSON error response.
|
||||
func writeError(c *gin.Context, err error) {
|
||||
code := util.CodeOf(err)
|
||||
var status int
|
||||
switch code {
|
||||
case util.CodeBadRequest:
|
||||
status = http.StatusBadRequest
|
||||
case util.CodeNotFound:
|
||||
status = http.StatusNotFound
|
||||
case util.CodeConflict:
|
||||
status = http.StatusConflict
|
||||
default:
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
msg := http.StatusText(status)
|
||||
if err != nil {
|
||||
msg = err.Error()
|
||||
}
|
||||
c.JSON(status, response.ErrorBody{Error: response.ErrorDetail{
|
||||
Code: string(code),
|
||||
Message: msg,
|
||||
}})
|
||||
}
|
||||
|
||||
// writeBadRequest is a shorthand for 400 responses without a service error.
|
||||
func writeBadRequest(c *gin.Context, message string) {
|
||||
c.JSON(http.StatusBadRequest, response.ErrorBody{Error: response.ErrorDetail{
|
||||
Code: string(util.CodeBadRequest),
|
||||
Message: message,
|
||||
}})
|
||||
}
|
||||
Reference in New Issue
Block a user