Skip to content

Commit

Permalink
feat(core): improve json format for CLI error (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-quere authored Jul 6, 2020
1 parent 0ce929f commit 30c9227
Show file tree
Hide file tree
Showing 23 changed files with 39 additions and 67 deletions.
16 changes: 11 additions & 5 deletions internal/core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ func (s *CliError) MarshalJSON() ([]byte, error) {
type emptyRes struct{}
return json.Marshal(&emptyRes{})
}

message := s.Message
if message == "" && s.Err != nil {
message = s.Err.Error()
}

type tmpRes struct {
Message string `json:"message"`
Error error `json:"error"`
Details string `json:"details"`
Hint string `json:"hint"`
Message string `json:"message,omitempty"`
Error error `json:"error,omitempty"`
Details string `json:"details,omitempty"`
Hint string `json:"hint,omitempty"`
}
return json.Marshal(&tmpRes{
Message: s.Message,
Message: message,
Error: s.Err,
Details: s.Details,
Hint: s.Hint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
Possible values: true, false
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "invalid value for 'is-happy' argument: invalid boolean value",
"error": {},
"details": "",
"hint": "Possible values: true, false"
}
3 changes: 1 addition & 2 deletions internal/e2e/testdata/test-human-get-invalid-arg-name.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
Valid arguments are: human-id, region
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "unknown argument 'invalid'",
"error": {},
"details": "",
"hint": "Valid arguments are: human-id, region"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ Cannot find resource 'human' with ID '11111111-1111-1111-1111-111111111111'
"error": {
"resource": "human",
"resource_id": "11111111-1111-1111-1111-111111111111"
},
"details": "",
"hint": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ Try again later :-)
"error": {
"resource": "ShoeSize60"
},
"details": "",
"hint": "Try again later :-)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
Try running: scw test human get 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 region=fr-par
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "a positional argument is required for this command",
"error": {},
"details": "",
"hint": "Try running: scw test human get 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 region=fr-par"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
Try running: scw test human update 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 eyes-color=red region=fr-par
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "a positional argument is required for this command",
"error": {},
"details": "",
"hint": "Try running: scw test human update 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 eyes-color=red region=fr-par"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ Server is not delivered
"message": "server is not delivered",
"error": {
"message": "server is not delivered"
},
"details": "",
"hint": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ Server is not delivered
"message": "server is not delivered",
"error": {
"message": "server is not delivered"
},
"details": "",
"hint": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ Server is not delivered
"message": "server is not delivered",
"error": {
"message": "server is not delivered"
},
"details": "",
"hint": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
No profile named test
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"error": {},
"details": "",
"hint": ""
"message": "no profile named test",
"error": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
No profile named test
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"error": {},
"details": "",
"hint": ""
"message": "no profile named test",
"error": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
No profile named test
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"error": {},
"details": "",
"hint": ""
"message": "no profile named test",
"error": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Details:
You must create an image of this volume and use its ID in the 'image' argument.
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "you cannot use an existing volume as a root volume",
"error": {},
"details": "You must create an image of this volume and use its ID in the 'image' argument.",
"hint": ""
"details": "You must create an image of this volume and use its ID in the 'image' argument."
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
Accepted values for 'type' are [GP1-XS GP1-S GP1-M GP1-L GP1-XL DEV1-S DEV1-M DEV1-L DEV1-XL RENDER-S]
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "invalid value 'MACBOOK1-S' for arg 'type'",
"error": {},
"details": "",
"hint": "Accepted values for 'type' are [GP1-XS GP1-S GP1-M GP1-L GP1-XL DEV1-S DEV1-M DEV1-L DEV1-XL RENDER-S]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
You must provide either a UUID ("11111111-1111-1111-1111-111111111111"), a local volume size ("local:100G" or "l:100G") or a block volume size ("block:100G" or "b:100G").
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "invalid volume format '20GB'",
"error": {},
"details": "",
"hint": "You must provide either a UUID (\"11111111-1111-1111-1111-111111111111\"), a local volume size (\"local:100G\" or \"l:100G\") or a block volume size (\"block:100G\" or \"b:100G\")."
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Details:
You must create an image of this volume and use its ID in the 'image' argument.
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "you cannot use an existing volume as a root volume",
"error": {},
"details": "You must create an image of this volume and use its ID in the 'image' argument.",
"hint": ""
"details": "You must create an image of this volume and use its ID in the 'image' argument."
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Missing required argument 'image'
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"error": {},
"details": "",
"hint": ""
"message": "missing required argument 'image'",
"error": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Invalid value for 'size' argument: size must be defined using the G or GB unit
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"error": {},
"details": "",
"hint": ""
"message": "invalid value for 'size' argument: size must be defined using the G or GB unit",
"error": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Hint:
Start the instance with: scw instance server start 4bf4ea6f-88a0-4b9b-bf08-9c4791bc29a2 --wait
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "",
"message": "server is not running",
"error": {},
"details": "",
"hint": "Start the instance with: scw instance server start 4bf4ea6f-88a0-4b9b-bf08-9c4791bc29a2 --wait"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ Cannot find resource '' with ID '11111111-1111-1111-1111-111111111111'
"error": {
"resource": "",
"resource_id": "11111111-1111-1111-1111-111111111111"
},
"details": "",
"hint": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ Cannot find resource '' with ID '11111111-1111-1111-1111-111111111111'
"error": {
"resource": "",
"resource_id": "11111111-1111-1111-1111-111111111111"
},
"details": "",
"hint": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ Cannot find resource '' with ID '11111111-1111-1111-1111-111111111111'
"error": {
"resource": "",
"resource_id": "11111111-1111-1111-1111-111111111111"
},
"details": "",
"hint": ""
}
}

0 comments on commit 30c9227

Please sign in to comment.