From: Tom Clegg Date: Thu, 13 Jun 2019 13:32:02 +0000 (-0400) Subject: 15000: Include bad value in error message. X-Git-Tag: 2.0.0~298^2~4 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/eaf26ebd285952ec6695270f0eda04ea7bf7e6c6 15000: Include bad value in error message. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- diff --git a/sdk/go/arvados/duration.go b/sdk/go/arvados/duration.go index 2696fdb051..ee482fdf31 100644 --- a/sdk/go/arvados/duration.go +++ b/sdk/go/arvados/duration.go @@ -20,7 +20,9 @@ func (d *Duration) UnmarshalJSON(data []byte) error { if data[0] == '"' { return d.Set(string(data[1 : len(data)-1])) } - return fmt.Errorf("duration must be given as a string like \"600s\" or \"1h30m\"") + // Mimic error message returned by ParseDuration for a number + // without units. + return fmt.Errorf("missing unit in duration %s", data) } // MarshalJSON implements json.Marshaler. diff --git a/sdk/go/arvados/duration_test.go b/sdk/go/arvados/duration_test.go index ee787a6a76..257a2b4ef5 100644 --- a/sdk/go/arvados/duration_test.go +++ b/sdk/go/arvados/duration_test.go @@ -43,3 +43,20 @@ func (s *DurationSuite) TestMarshalJSON(c *check.C) { c.Check(string(buf), check.Equals, `"`+trial.out+`"`) } } + +func (s *DurationSuite) TestUnmarshalJSON(c *check.C) { + var d struct { + D Duration + } + err := json.Unmarshal([]byte(`{"D":1.234}`), &d) + c.Check(err, check.ErrorMatches, `missing unit in duration 1.234`) + err = json.Unmarshal([]byte(`{"D":"1.234"}`), &d) + c.Check(err, check.ErrorMatches, `.*missing unit in duration 1.234`) + err = json.Unmarshal([]byte(`{"D":"1"}`), &d) + c.Check(err, check.ErrorMatches, `.*missing unit in duration 1`) + err = json.Unmarshal([]byte(`{"D":"foobar"}`), &d) + c.Check(err, check.ErrorMatches, `.*invalid duration foobar`) + err = json.Unmarshal([]byte(`{"D":"60s"}`), &d) + c.Check(err, check.IsNil) + c.Check(d.D.Duration(), check.Equals, time.Minute) +}