Skip to content

Commit

Permalink
cmd: Print meaningful error messages on network issues (ory#1493)
Browse files Browse the repository at this point in the history
Closes ory#1492
  • Loading branch information
aeneasr authored Jul 19, 2019
1 parent e4bac7e commit deb1574
Show file tree
Hide file tree
Showing 57 changed files with 236 additions and 202 deletions.
36 changes: 36 additions & 0 deletions cmd/cli/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"bytes"
"encoding/json"
)

func formatSwaggerError(err error) string {
if err == nil {
return ""
}

var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(err); err != nil {
panic(err)
}

var e struct {
Payload json.RawMessage
}
if err := json.NewDecoder(&b).Decode(&e); err != nil {
panic(err)
}

if len(e.Payload) == 0 {
return err.Error()
}

dec := json.NewEncoder(&b)
dec.SetIndent("", " ")
if err := dec.Encode(e.Payload); err != nil {
panic(err)
}

return b.String()
}
10 changes: 5 additions & 5 deletions cmd/cli/handler_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {
cmdx.Must(err, "Could not parse JSON from file %s: %s", path, err)

response, err := m.Admin.CreateOAuth2Client(admin.NewCreateOAuth2ClientParams().WithBody(&c))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
result := response.Payload

if c.Secret == "" {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
}

response, err := m.Admin.CreateOAuth2Client(admin.NewCreateOAuth2ClientParams().WithBody(&cc))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
result := response.Payload

fmt.Printf("OAuth 2.0 Client ID: %s\n", result.ClientID)
Expand Down Expand Up @@ -153,7 +153,7 @@ func (h *ClientHandler) DeleteClient(cmd *cobra.Command, args []string) {

for _, c := range args {
_, err := m.Admin.DeleteOAuth2Client(admin.NewDeleteOAuth2ClientParams().WithID(c))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
}

fmt.Println("OAuth2 client(s) deleted")
Expand All @@ -168,7 +168,7 @@ func (h *ClientHandler) GetClient(cmd *cobra.Command, args []string) {
}

response, err := m.Admin.GetOAuth2Client(admin.NewGetOAuth2ClientParams().WithID(args[0]))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
cl := response.Payload
fmt.Println(cmdx.FormatResponse(cl))
}
Expand All @@ -181,7 +181,7 @@ func (h *ClientHandler) ListClients(cmd *cobra.Command, args []string) {
offset := (limit * page) - limit

response, err := m.Admin.ListOAuth2Clients(admin.NewListOAuth2ClientsParams().WithLimit(pointerx.Int64(int64(limit))).WithOffset(pointerx.Int64(int64(offset))))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
cls := response.Payload

table := newTable()
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/handler_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ func (h *IntrospectionHandler) Introspect(cmd *cobra.Command, args []string) {
WithScope(pointerx.String(strings.Join(flagx.MustGetStringSlice(cmd, "scope"), " "))),
ht,
)
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
fmt.Println(formatResponse(result.Payload))
}
6 changes: 3 additions & 3 deletions cmd/cli/handler_jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (h *JWKHandler) CreateKeys(cmd *cobra.Command, args []string) {
KeyID: pointerx.String(kid),
Use: pointerx.String(flagx.MustGetString(cmd, "use")),
}))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
fmt.Println(formatResponse(res.Payload))
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (h *JWKHandler) GetKeys(cmd *cobra.Command, args []string) {
m := configureClient(cmd)

keys, err := m.Admin.GetJSONWebKeySet(admin.NewGetJSONWebKeySetParams().WithSet(args[0]))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
fmt.Printf("%s\n", formatResponse(keys))
}

Expand All @@ -184,6 +184,6 @@ func (h *JWKHandler) DeleteKeys(cmd *cobra.Command, args []string) {
m := configureClient(cmd)

_, err := m.Admin.DeleteJSONWebKeySet(admin.NewDeleteJSONWebKeySetParams().WithSet(args[0]))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
fmt.Printf("JSON Web Key Set deleted: %s\n", args[0])
}
4 changes: 2 additions & 2 deletions cmd/cli/handler_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Please provide a Client ID and Client Secret using flags --client-id and --clien

token := args[0]
_, err := handler.Public.RevokeOAuth2Token(public.NewRevokeOAuth2TokenParams().WithToken(args[0]), httptransport.BasicAuth(clientID, clientSecret))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))

fmt.Printf("Revoked OAuth 2.0 Access Token: %s\n", token)
}
Expand All @@ -69,6 +69,6 @@ func (h *TokenHandler) FlushTokens(cmd *cobra.Command, args []string) {
_, err := handler.Admin.FlushInactiveOAuth2Tokens(admin.NewFlushInactiveOAuth2TokensParams().WithBody(&models.FlushInactiveOAuth2TokensRequest{
NotAfter: strfmt.DateTime(time.Now().Add(-flagx.MustGetDuration(cmd, "min-age"))),
}))
cmdx.Must(err, "Unable to execute request: %s", err)
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
fmt.Println("Successfully flushed inactive access tokens")
}
8 changes: 4 additions & 4 deletions doc_swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ type genericError struct {
// example: The requested resource could not be found
Name string `json:"error"`

// Hint contains further information on the nature of the error.
// Description contains further information on the nature of the error.
//
// example: Object with ID 12345 does not exist
Hint string `json:"error_hint"`
Description string `json:"error_description"`

// Code represents the error status code (404, 403, 401, ...).
//
// example: 404
Code int `json:"error_code"`
Code int `json:"status_code"`

// Debug contains debug information. This is usually not available and has to be enabled.
//
// example: The database adapter was unable to find the element
Debug string `json:"error_debug"`
Debug string `json:"debug"`
}

// Empty responses are sent when, for example, resources are deleted. The HTTP status code for empty responses is
Expand Down
26 changes: 13 additions & 13 deletions docs/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2194,30 +2194,30 @@
"error"
],
"properties": {
"debug": {
"description": "Debug contains debug information. This is usually not available and has to be enabled.",
"type": "string",
"x-go-name": "Debug",
"example": "The database adapter was unable to find the element"
},
"error": {
"description": "Name is the error name.",
"type": "string",
"x-go-name": "Name",
"example": "The requested resource could not be found"
},
"error_code": {
"error_description": {
"description": "Description contains further information on the nature of the error.",
"type": "string",
"x-go-name": "Description",
"example": "Object with ID 12345 does not exist"
},
"status_code": {
"description": "Code represents the error status code (404, 403, 401, ...).",
"type": "integer",
"format": "int64",
"x-go-name": "Code",
"example": 404
},
"error_debug": {
"description": "Debug contains debug information. This is usually not available and has to be enabled.",
"type": "string",
"x-go-name": "Debug",
"example": "The database adapter was unable to find the element"
},
"error_hint": {
"description": "Hint contains further information on the nature of the error.",
"type": "string",
"x-go-name": "Hint",
"example": "Object with ID 12345 does not exist"
}
},
"x-go-package": "github.com/ory/hydra"
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require (
cloud.google.com/go v0.39.0 // indirect
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/go-openapi/analysis v0.19.0 // indirect
github.com/go-openapi/errors v0.18.0
github.com/go-openapi/inflect v0.19.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0
github.com/go-bindata/go-bindata v3.1.1+incompatible h1:tR4f0e4VTO7LK6B2YWyAoVEzG9ByG1wrXB4TL9+jiYg=
github.com/go-bindata/go-bindata v3.1.1+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-openapi/analysis v0.0.0-20180825180245-b006789cd277/go.mod h1:k70tL6pCuVxPJOHXQ+wIac1FUrvNkHolPie/cLEU6hI=
Expand Down
8 changes: 4 additions & 4 deletions sdk/go/hydra/models/generic_error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions sdk/java/hydra-client-resttemplate/docs/GenericError.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**debug** | **String** | Debug contains debug information. This is usually not available and has to be enabled. | [optional]
**error** | **String** | Name is the error name. |
**errorCode** | **Long** | Code represents the error status code (404, 403, 401, ...). | [optional]
**errorDebug** | **String** | Debug contains debug information. This is usually not available and has to be enabled. | [optional]
**errorHint** | **String** | Hint contains further information on the nature of the error. | [optional]
**errorDescription** | **String** | Description contains further information on the nature of the error. | [optional]
**statusCode** | **Long** | Code represents the error status code (404, 403, 401, ...). | [optional]



Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import com.github.ory.hydra.auth.ApiKeyAuth;
import com.github.ory.hydra.auth.OAuth;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
@Component("com.github.ory.hydra.ApiClient")
public class ApiClient {
public enum CollectionFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
@Component("com.github.ory.hydra.api.AdminApi")
public class AdminApi {
private ApiClient apiClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
@Component("com.github.ory.hydra.api.HealthApi")
public class HealthApi {
private ApiClient apiClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
@Component("com.github.ory.hydra.api.PublicApi")
public class PublicApi {
private ApiClient apiClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
@Component("com.github.ory.hydra.api.VersionApi")
public class VersionApi {
private ApiClient apiClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class ApiKeyAuth implements Authentication {
private final String location;
private final String paramName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.util.Base64Utils;
import org.springframework.util.MultiValueMap;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class OAuth implements Authentication {
private String accessToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* AcceptConsentRequest
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class AcceptConsentRequest {
@JsonProperty("grant_access_token_audience")
private List<String> grantAccessTokenAudience = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* AcceptLoginRequest
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class AcceptLoginRequest {
@JsonProperty("acr")
private String acr = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* CompletedRequest
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class CompletedRequest {
@JsonProperty("redirect_to")
private String redirectTo = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* ConsentRequest
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class ConsentRequest {
@JsonProperty("acr")
private String acr = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* ConsentRequestSession
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class ConsentRequestSession {
@JsonProperty("access_token")
private Map<String, Object> accessToken = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Empty responses are sent when, for example, resources are deleted. The HTTP status code for empty responses is typically 201.
*/
@ApiModel(description = "Empty responses are sent when, for example, resources are deleted. The HTTP status code for empty responses is typically 201.")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class EmptyResponse {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* FlushInactiveOAuth2TokensRequest
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-06-11T14:10:17.875+02:00")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-07-18T19:30:59.329+02:00")
public class FlushInactiveOAuth2TokensRequest {
@JsonProperty("notAfter")
private DateTime notAfter = null;
Expand Down
Loading

0 comments on commit deb1574

Please sign in to comment.