Skip to content
Open
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
5 changes: 5 additions & 0 deletions pkg/server/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (e *Error) Write(w http.ResponseWriter, r *http.Request) {
}
}

// FromOpenAPIError allows propagation across API calls.
func FromOpenAPIError(code int, err *openapi.Error) *Error {
return newError(code, err.Error, err.ErrorDescription)
}

// HTTPForbidden is raised when a user isn't permitted to do something by RBAC.
func HTTPForbidden(description string) *Error {
return newError(http.StatusForbidden, openapi.Forbidden, description)
Expand Down
20 changes: 11 additions & 9 deletions pkg/util/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ limitations under the License.
package api

import (
"errors"
goerrors "errors"
"fmt"
"reflect"

"github.com/unikorn-cloud/core/pkg/openapi"
"github.com/unikorn-cloud/core/pkg/server/errors"
)

var (
ErrExtraction = errors.New("api error extraction error")

ErrAPI = errors.New("api error")
ErrExtraction = goerrors.New("api error extraction error")
)

// ExtractError provides a response type agnostic way of extracting a human readable
Expand All @@ -51,17 +50,20 @@ func ExtractError(statusCode int, response any) error {
// ... that through the magic of autogeneration has a field for the status code ...
fieldName := fmt.Sprintf("JSON%d", statusCode)

v = v.FieldByName(fieldName)

if v.IsZero() {
f := v.FieldByName(fieldName)
if !f.IsValid() {
return fmt.Errorf("%w: error field %s not defined", ErrExtraction, fieldName)
}

if f.IsZero() {
return fmt.Errorf("%w: error field %s not populated", ErrExtraction, fieldName)
}

// ... which points to an Error.
concreteError, ok := v.Interface().(*openapi.Error)
concreteError, ok := f.Interface().(*openapi.Error)
if !ok {
return fmt.Errorf("%w: unable to assert error", ErrExtraction)
}

return fmt.Errorf("%w: %v - %v", ErrAPI, concreteError.Error, concreteError.ErrorDescription)
return errors.FromOpenAPIError(statusCode, concreteError)
}
85 changes: 85 additions & 0 deletions pkg/util/api/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2025 the Unikorn Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api_test

import (
"net/http"
"testing"

"github.com/stretchr/testify/require"

"github.com/unikorn-cloud/core/pkg/openapi"
"github.com/unikorn-cloud/core/pkg/server/errors"
"github.com/unikorn-cloud/core/pkg/util/api"
)

const (
description = "this is a test"
)

type openapiResponseFixture struct {
JSON400 *openapi.Error
}

// TestExtractError ensures errors are correctly extracted an propagated.
func TestExtractError(t *testing.T) {
t.Parallel()

resp := &openapiResponseFixture{
JSON400: &openapi.Error{
Error: openapi.InvalidRequest,
ErrorDescription: description,
},
}

err := api.ExtractError(http.StatusBadRequest, resp)
require.Error(t, err, "must return an error")

var apiError *errors.Error

require.ErrorAs(t, err, &apiError, "must be an API error")
}

// TestExtractErrorUnknownCode ensures we can handle something that is unexpected
// e.g. an ingress going wrong.
func TestExtractErrorUnknownCode(t *testing.T) {
t.Parallel()

resp := &openapiResponseFixture{}

err := api.ExtractError(http.StatusBadGateway, resp)
require.Error(t, err, "must return an error")

var apiError *errors.Error

require.NotErrorAs(t, err, &apiError, "must not be an API error")
}

// TestExtractErrorUnpopulatedCode ensures we can handle something that should be
// populated but isn't e.g. faulty API error handling.
func TestExtractErrorUnpopulatedCode(t *testing.T) {
t.Parallel()

resp := &openapiResponseFixture{}

err := api.ExtractError(http.StatusBadRequest, resp)
require.Error(t, err, "must return an error")

var apiError *errors.Error

require.NotErrorAs(t, err, &apiError, "must not be an API error")
}