Skip to content

Commit

Permalink
errors: implement GoStringer
Browse files Browse the repository at this point in the history
now Error returns singleline
and GoString return multiline with reasons

before this, Error used to return multine with reasons

MessageFmt is deprecated, use Error method instead
  • Loading branch information
santhosh-tekuri committed Aug 2, 2021
1 parent 0ab66a4 commit 86bccb1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/jv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
err = schema.Validate(r)
_ = r.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "%q does not conform to the schema specified. reason:\n%v\n", f, err)
fmt.Fprintf(os.Stderr, "%q does not conform to the schema specified. reason:\n%#v\n", f, err)
os.Exit(1)
}
}
Expand Down
24 changes: 17 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

// InvalidJSONTypeError is the error type returned by ValidateInteface.
// InvalidJSONTypeError is the error type returned by ValidateInterface.
// this tells that specified go object is not valid jsonType.
type InvalidJSONTypeError string

Expand All @@ -30,7 +30,11 @@ type SchemaError struct {
}

func (se *SchemaError) Error() string {
return fmt.Sprintf("json-schema %q compilation failed. Reason:\n%s", se.SchemaURL, se.Err)
return fmt.Sprintf("json-schema %q compilation failed", se.SchemaURL)
}

func (se *SchemaError) GoString() string {
return fmt.Sprintf("json-schema %q compilation failed. Reason:\n%#v", se.SchemaURL, se.Err)
}

// ValidationError is the error type returned by Validate.
Expand All @@ -56,21 +60,27 @@ type ValidationError struct {

func (ve *ValidationError) add(causes ...error) error {
for _, cause := range causes {
addContext(ve.InstancePtr, ve.SchemaPtr, cause)
_ = addContext(ve.InstancePtr, ve.SchemaPtr, cause)
ve.Causes = append(ve.Causes, cause.(*ValidationError))
}
return ve
}

// MessageFmt returns the Message formatted, but does not include child Cause messages.
//
// Deprecated: use Error method
func (ve *ValidationError) MessageFmt() string {
return fmt.Sprintf("I[%s] S[%s] %s", ve.InstancePtr, ve.SchemaPtr, ve.Message)
return ve.Error()
}

func (ve *ValidationError) Error() string {
msg := ve.MessageFmt()
return fmt.Sprintf("I[%s] S[%s] %s", ve.InstancePtr, ve.SchemaPtr, ve.Message)
}

func (ve *ValidationError) GoString() string {
msg := ve.Error()
for _, c := range ve.Causes {
for _, line := range strings.Split(c.Error(), "\n") {
for _, line := range strings.Split(c.GoString(), "\n") {
msg += "\n " + line
}
}
Expand All @@ -88,7 +98,7 @@ func addContext(instancePtr, schemaPtr string, err error) error {
ve.SchemaPtr = joinPtr(schemaPtr, ve.SchemaPtr)
}
for _, cause := range ve.Causes {
addContext(instancePtr, schemaPtr, cause)
_ = addContext(instancePtr, schemaPtr, cause)
}
return ve
}
Expand Down
2 changes: 1 addition & 1 deletion schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func testFolder(t *testing.T, folder string, draft *jsonschema.Draft) {
err = schema.Validate(bytes.NewReader(test.Data))
valid := err == nil
if !valid {
for _, line := range strings.Split(err.Error(), "\n") {
for _, line := range strings.Split(err.(*jsonschema.ValidationError).GoString(), "\n") {
t.Logf(" %s\n", line)
}
}
Expand Down

0 comments on commit 86bccb1

Please sign in to comment.