Skip to content

JSONLogger let's error interface take priority over json.Marshaler #580

Closed
@cam-stitt

Description

From what I can see, if you have an object that implements both json.Marshaler and error, it will always use the error interface. This is despite the fact that code states that json.Marshaler should always win here. Consider this (I know it's not perfect, but we have a stack trace in our error that we want logged):

type Error struct {
  Message string
  Status int
}

func (e Error) Error() string {
  return e.Message
}

func (e Error) MarshalJSON() ([]byte, error) {
  aux := struct{Message string, Status Int}{Message: e.Message, Status: e.Status}
  return json.Marshal(aux)
}

And you were to log it:

err := Error{msg: "Error impl", Status: 200}
logger.Log("msg", err)

The result will always be {"err": "Error impl"}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions