Skip to content

Commit

Permalink
feature/add apiv1 error response-style (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindHunter86 authored Jul 16, 2024
2 parents 5461c22 + 91f853d commit 12db3f4
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 6 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/klauspost/compress v1.17.9
github.com/mailru/easyjson v0.7.7
github.com/rs/zerolog v1.33.0
github.com/urfave/cli/v2 v2.27.2
github.com/valyala/bytebufferpool v1.0.0
Expand All @@ -17,6 +18,7 @@ require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+UV8OU=
github.com/jedib0t/go-pretty/v6 v6.5.9/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
42 changes: 42 additions & 0 deletions internal/service/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package service

import (
"io"

easyjson "github.com/mailru/easyjson"
)

type (
FiberErrorResponse struct {
Status bool
Data interface{}
Error *ResponseError
}
ResponseError struct {
Code int
Message string
Description string
}
)

func newFiberResponseError(status int, msg, desc string) *FiberErrorResponse {
return &FiberErrorResponse{
Error: &ResponseError{
Code: status,
Message: msg,
Description: desc,
},
}
}

func respondWithError(status int, msg, desc string, w io.Writer) (e error) {
err := newFiberResponseError(status, msg, desc)

var buf []byte
if buf, e = easyjson.Marshal(err); e != nil {
return
}

_, e = w.Write(buf)
return
}
203 changes: 203 additions & 0 deletions internal/service/error_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,30 @@ func NewService(c *cli.Context, l *zerolog.Logger, s io.Writer) *Service {
return c.Context().Conn().Close()
}

c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)

var e *fiber.Error
if !errors.As(err, &e) {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
// AniLibria apiv1 error style:
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)

// `rspcode` - apiv1 legacy hardcode
// if u have 4XX or 5XX in service, u must respond with 200
rspcode, respdesc, respond :=
fiber.StatusOK,
"error provided by alice ("+gCli.App.Version+") service",
func(status int, msg, desc string) {
if e := respondWithError(status, msg, desc, c); e != nil {
rlog(c).Error().Msg("could not respond with JSON error - " + e.Error())
}
}

// parse fiber error
var ferr *fiber.Error
if !errors.As(err, &ferr) {
respond(fiber.StatusInternalServerError, err.Error(), "")
return c.SendStatus(rspcode)
}

respond(ferr.Code, ferr.Error(), respdesc)
rlog(c).Error().Msgf("%+v", err)
return c.SendStatus(e.Code)
return c.SendStatus(rspcode)
},
})

Expand Down

0 comments on commit 12db3f4

Please sign in to comment.