mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-04 19:29:49 -06:00
always use error interface when returning errors (#8816)
* always use error interface when returning errors Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * fix: use non pointer Error * fix: errorcode evaluation --------- Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> Co-authored-by: Florian Schade <f.schade@icloud.com>
This commit is contained in:
committed by
GitHub
parent
fad37db840
commit
7a635738fa
45
services/graph/pkg/errorcode/errorcode_test.go
Normal file
45
services/graph/pkg/errorcode/errorcode_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package errorcode_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/owncloud/ocis/v2/services/graph/pkg/errorcode"
|
||||
)
|
||||
|
||||
type customErr struct{}
|
||||
|
||||
func (customErr) Error() string {
|
||||
return "some error"
|
||||
}
|
||||
|
||||
func TestRenderError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("errorcode.Error value error", func(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
err := errorcode.New(errorcode.ItemNotFound, "test error")
|
||||
errorcode.RenderError(w, r, err)
|
||||
require.Equal(t, http.StatusNotFound, w.Code)
|
||||
})
|
||||
|
||||
t.Run("errorcode.Error zero value error", func(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
var err errorcode.Error
|
||||
errorcode.RenderError(w, r, err)
|
||||
require.Equal(t, http.StatusForbidden, w.Code)
|
||||
})
|
||||
|
||||
t.Run("custom error", func(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
var err customErr
|
||||
errorcode.RenderError(w, r, err)
|
||||
require.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user