ent/ent · error
graphson.RawMessage: UnmarshalGraphson on nil pointer
Error message
graphson.RawMessage: UnmarshalGraphson on nil pointer
What it means
UnmarshalGraphson is defined on a *RawMessage, and Go allows calling methods through a nil pointer receiver. This guard fires when the method is invoked on a nil *RawMessage, i.e. the caller never allocated the RawMessage (or holds a nil interface/pointer) before asking it to absorb the decoded bytes. The offending input is the nil receiver m, not the data argument.
Source
Thrown at dialect/gremlin/encoding/graphson/raw.go:31
// RawMessage must implement Marshaler/Unmarshaler interfaces.
var (
_ Marshaler = (*RawMessage)(nil)
_ Unmarshaler = (*RawMessage)(nil)
)
// MarshalGraphson returns m as the graphson encoding of m.
func (m RawMessage) MarshalGraphson() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalGraphson sets *m to a copy of data.
func (m *RawMessage) UnmarshalGraphson(data []byte) error {
if m == nil {
return errors.New("graphson.RawMessage: UnmarshalGraphson on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}
View on GitHub (pinned to 69d5d4deb1)
Solutions
- Initialize the value: `m := graphson.RawMessage{}` and call on the addressable value, or `m = &graphson.RawMessage{}`
- Use a non-pointer `graphson.RawMessage` field/type in your target struct
Example fix
// before
var m *graphson.RawMessage
m.UnmarshalGraphson(data)
// after
m := &graphson.RawMessage{}
m.UnmarshalGraphson(data) Defensive patterns
Strategy: type-guard
Validate before calling
if m == nil { return errors.New("RawMessage receiver must be initialized") } Type guard
func safeUnmarshal(m *graphson.RawMessage, data []byte) error {
if m == nil { m = &graphson.RawMessage{} }
return m.UnmarshalGraphson(data)
} Try / catch
if err := m.UnmarshalGraphson(data); err != nil {
return fmt.Errorf("raw graphson: %w", err)
} Prevention
- Use value type graphson.RawMessage, not *RawMessage, in structs
- Initialize pointer fields before decoding
- Never call UnmarshalGraphson on a freshly declared nil pointer
When it happens
Trigger: Declaring `var m *graphson.RawMessage` (nil pointer) and calling `m.UnmarshalGraphson(data)`; having a struct field of type *RawMessage left nil when the decoder calls into it.
Common situations: Using a pointer-to-pointer or nil struct field in a response type; forgetting to initialize a RawMessage field before decoding.
Related errors
- expect map element, but found only key
- missing type or value
- cannot unmarshal into a nil pointer
- cannot read first list element: %w
- cannot unmarshal first map item: %w
AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03).
Data as JSON: /api/errors/d39d924c2751ab4b.
Report an issue: GitHub.