Skip to content

Commit

Permalink
fix: request error rest-server return (#91)
Browse files Browse the repository at this point in the history
dantasrafael authored Jun 21, 2024
1 parent 54f6351 commit 9a9c199
Showing 3 changed files with 18 additions and 21 deletions.
10 changes: 3 additions & 7 deletions pkg/web/restserver/fiber_server.go
Original file line number Diff line number Diff line change
@@ -2,14 +2,15 @@ package restserver

import (
"fmt"
"strings"
"time"

"github.com/colibri-project-io/colibri-sdk-go/pkg/base/config"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/logging"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
"strings"
"time"
)

type fiberWebServer struct {
@@ -88,11 +89,6 @@ func (f *fiberWebServer) injectRoutes() {
}

fn(webContext)

if webContext.err != nil {
return webContext.err
}

return nil
})

14 changes: 6 additions & 8 deletions pkg/web/restserver/fiber_web_context.go
Original file line number Diff line number Diff line change
@@ -3,18 +3,18 @@ package restserver
import (
"context"
"encoding/json"
"mime/multipart"
"net/http"
"strings"

"github.com/colibri-project-io/colibri-sdk-go/pkg/base/logging"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/security"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/validator"
"github.com/gofiber/fiber/v2"
"mime/multipart"
"net/http"
"strings"
)

type fiberWebContext struct {
ctx *fiber.Ctx
err error
}

func newFiberWebContext(ctx *fiber.Ctx) *fiberWebContext {
@@ -94,16 +94,14 @@ func (f *fiberWebContext) ServeFile(path string) {
}

func (f *fiberWebContext) JsonResponse(statusCode int, body any) {
f.ctx.Response().SetStatusCode(statusCode)
f.ctx.Status(statusCode)
if err := f.ctx.JSON(body); err != nil {
f.ErrorResponse(http.StatusInternalServerError, err)
}
}

func (f *fiberWebContext) ErrorResponse(statusCode int, err error) {
f.ctx.Response().SetStatusCode(statusCode)
_ = f.ctx.JSON(Error{err.Error()})
f.err = err
f.JsonResponse(statusCode, Error{err.Error()})
}

func (f *fiberWebContext) EmptyResponse(statusCode int) {
15 changes: 9 additions & 6 deletions pkg/web/restserver/server_test.go
Original file line number Diff line number Diff line change
@@ -421,7 +421,9 @@ func TestStartRestServer(t *testing.T) {
})

t.Run("Should return error response body", func(t *testing.T) {
response := restclient.Request[Error, any]{
expected := &Error{"test-error-body"}

response := restclient.Request[any, Error]{
Ctx: ctx,
Client: client,
HttpMethod: http.MethodGet,
@@ -431,7 +433,8 @@ func TestStartRestServer(t *testing.T) {
assert.NotNil(t, response)
assert.EqualValues(t, http.StatusInternalServerError, response.StatusCode())
assert.Nil(t, response.SuccessBody())
assert.Nil(t, response.ErrorBody())
assert.NotNil(t, response.ErrorBody())
assert.EqualValues(t, expected, response.ErrorBody())
assert.Error(t, errors.New("500 statusCode"), response.Error())
})

@@ -543,7 +546,7 @@ func TestStartRestServer(t *testing.T) {
})

t.Run("Should return bad request when an error occurred in decoded body", func(t *testing.T) {
response := restclient.Request[Resp, any]{
response := restclient.Request[Resp, Error]{
Ctx: ctx,
Client: client,
HttpMethod: http.MethodPost,
@@ -552,10 +555,10 @@ func TestStartRestServer(t *testing.T) {
}.Call()

assert.NotNil(t, response)
assert.EqualValues(t, http.StatusInternalServerError, response.StatusCode())
assert.EqualValues(t, http.StatusBadRequest, response.StatusCode())
assert.Nil(t, response.SuccessBody())
assert.Nil(t, response.ErrorBody())
assert.ErrorContains(t, response.Error(), "500 status code.")
assert.NotNil(t, response.ErrorBody())
assert.ErrorContains(t, response.Error(), "400 status code")
})

t.Run("Should return decoded body", func(t *testing.T) {

0 comments on commit 9a9c199

Please sign in to comment.