Return JSON-encoded errors
This commit is contained in:
32
internal/errorx/http.go
Normal file
32
internal/errorx/http.go
Normal file
@ -0,0 +1,32 @@
|
||||
package errorx
|
||||
|
||||
const defaultCode = 1001
|
||||
|
||||
type CodeError struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type CodeErrorResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func NewCodeError(code int, msg string) error {
|
||||
return &CodeError{Code: code, Msg: msg}
|
||||
}
|
||||
|
||||
func NewDefaultError(msg string) error {
|
||||
return NewCodeError(defaultCode, msg)
|
||||
}
|
||||
|
||||
func (e *CodeError) Error() string {
|
||||
return e.Msg
|
||||
}
|
||||
|
||||
func (e *CodeError) Data() *CodeErrorResponse {
|
||||
return &CodeErrorResponse{
|
||||
Code: e.Code,
|
||||
Msg: e.Msg,
|
||||
}
|
||||
}
|
@ -2,9 +2,9 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"5feet11/internal/db"
|
||||
"5feet11/internal/errorx"
|
||||
"5feet11/internal/svc"
|
||||
"5feet11/internal/types"
|
||||
|
||||
@ -31,11 +31,11 @@ func (l *ExpandUrlLogic) ExpandUrl(req *types.ExpandReq) (resp *types.ExpandResp
|
||||
|
||||
var urls []db.UrlModel
|
||||
if err := queryUrl.Select(&urls); err != nil {
|
||||
return nil, err
|
||||
return nil, errorx.NewDefaultError(err.Error())
|
||||
}
|
||||
|
||||
if len(urls) != 1 {
|
||||
return nil, fmt.Errorf("no URL found")
|
||||
return nil, errorx.NewDefaultError("no URL found")
|
||||
}
|
||||
|
||||
resp = &types.ExpandResp{
|
||||
|
@ -2,9 +2,9 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"5feet11/internal/db"
|
||||
"5feet11/internal/errorx"
|
||||
"5feet11/internal/svc"
|
||||
"5feet11/internal/types"
|
||||
|
||||
@ -31,11 +31,11 @@ func (l *GetLinkLogic) GetLink(req *types.ExpandReq) (resp *types.GetLinkResp, e
|
||||
|
||||
var urls []db.UrlModel
|
||||
if err := queryUrl.Select(&urls); err != nil {
|
||||
return nil, err
|
||||
return nil, errorx.NewDefaultError(err.Error())
|
||||
}
|
||||
|
||||
if len(urls) != 1 {
|
||||
return nil, fmt.Errorf("no URL found")
|
||||
return nil, errorx.NewDefaultError("no URL found")
|
||||
}
|
||||
|
||||
resp = &types.GetLinkResp{
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"5feet11/internal/db"
|
||||
"5feet11/internal/errorx"
|
||||
"5feet11/internal/svc"
|
||||
"5feet11/internal/types"
|
||||
|
||||
@ -43,7 +44,7 @@ func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.Shorten
|
||||
})
|
||||
|
||||
if err := insertUrl.ExecRelease(); err != nil {
|
||||
return nil, err
|
||||
return nil, errorx.NewDefaultError(err.Error())
|
||||
}
|
||||
|
||||
resp = &types.ShortenResp{
|
||||
|
Reference in New Issue
Block a user