Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: request error rest-server return #91

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions pkg/web/restserver/fiber_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,11 +89,6 @@ func (f *fiberWebServer) injectRoutes() {
}

fn(webContext)

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

return nil
})

Expand Down
14 changes: 6 additions & 8 deletions pkg/web/restserver/fiber_web_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 9 additions & 6 deletions pkg/web/restserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())
})

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
Loading