panic: errors: *target must be interface or implement error in Go


I am making a json unmarshalling error handling function in Go:

import ""

func parseJSONError(err error) {
    var uterr json.UnmarshalTypeError

    if errors.As(err, &uterr) {

    var serr json.SyntaxError

    if errors.As(err, &serr) {

But there is a panic in errors.As(): panic: errors: *target must be interface or implement error.

What is target we can learn from the documentation:

func As(err error, target interface{}) bool

The problem is that both json.UnmarshalTypeError and json.SyntaxError actually implement the error interface. We can learn it from the encoding/json documentation. So I do not have any idea what I am doing wrong. Even explicit casting uterr and serr to the interface{} does not save the situation.

The panic occurs in both and standard errors packages.


The documentation of errors.As states:

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

So you have to consider the following:

  1. json.UnmarshalTypeError does not implement error.
  2. *json.UnmarshalTypeError does, because the method Error() string has a pointer receiver (docs)
  3. based on the documentation, errors.As wants a pointer to what implements error, so you need **json.UnmarshalTypeError

Change the code to:

uterr := &json.UnmarshalTypeError{}
if errors.As(err, &uterr) {
    // ...

Answered By – blackgreen

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.