Skip to content

Mongo PubSub issue #2

@vladbucur1

Description

@vladbucur1

Returning a typed nil from NewHTTPError causes err != nil to be true, leading to panics

Context
In the codebase, we have:

func (m *mongoPubSub) Publish(ctx context.Context, _ types.RealmID, tenant string, event EventMessage) error {
// ...
return types.NewHTTPError(http.StatusInternalServerError, err)
}

The NewHTTPError function is defined as:

func NewHTTPError(code int, e error) *HTTPError {
if e == nil {
return nil
}
// ...
}

So, when e is nil, NewHTTPError returns a *HTTPError that is nil (typed nil pointer).
However, when Publish returns this value, it is assigned to an error interface in the caller:

err := s.inner.Publish(ctx, realm, tenant, event)
return otel.RecordOutcome(err, span)

Problem

  • Inside Publish, error is a *types.HTTPError and is <nil>.
  • When returned, it is assigned to an error interface, which now has a non-nil type (*types.HTTPError) but a nil value.
  • This means err != nil evaluates to true, even though the value is nil.
  • This leads to code like err.Error() panicking, since it tries to call a method on a nil pointer.

Solution:
Handle Mongo similar to GCP:

func (m *mongoPubSub) Publish(ctx context.Context, _ types.RealmID, tenant string, event EventMessage) error {
// ...
_, err = collection.InsertOne(ctx, me)
if err != nil {
return types.NewHTTPError(http.StatusInternalServerError, err)
}
return nil
}

@Imperiopolis, what do you think?

Metadata

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