...

Source file src/github.com/ory/x/swaggerx/error.go

Documentation: github.com/ory/x/swaggerx

     1  package swaggerx
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	"github.com/go-openapi/runtime"
    10  )
    11  
    12  func FormatSwaggerError(err error) string {
    13  	var e *runtime.APIError
    14  	if errors.As(err, &e) {
    15  		body, err := json.MarshalIndent(e, "\t", "  ")
    16  		if err != nil {
    17  			body = []byte(fmt.Sprintf("%+v", e.Response))
    18  		}
    19  
    20  		switch e.Code {
    21  		case http.StatusForbidden:
    22  			return fmt.Sprintf("The service responded with status code 403 indicating that you lack permission to access the resource. The full error details are:\n\n\t%s\n\n", body)
    23  		case http.StatusUnauthorized:
    24  			return fmt.Sprintf("The service responded with status code 401 indicating that you forgot to include credentials (e.g. token, TLS certificate, ...) in the HTTP request.  The full error details are:\n\n\t%s\n\n", body)
    25  		case http.StatusNotFound:
    26  			return fmt.Sprintf("The service responded with status code 404 indicating that the resource does not exist. Check that the URL is correct (are you using the correct admin/public/... endpoint?) and that the resource exists. The full error details are:\n\n\t%s\n\n", body)
    27  		default:
    28  			return fmt.Sprintf("Unable to complete operation %s because the server responded with status code %d:\n\n\t%s\n", e.OperationName, e.Code, body)
    29  		}
    30  	}
    31  	return fmt.Sprintf("%+v", err)
    32  }
    33  

View as plain text