Skip to content

Dynamic registration of error formatters causes global overrides #2591

@NobodysNightmare

Description

@NobodysNightmare

Hey there, we started receiving the following warning after a grape update to our application:

json is already registered with class OurSpecificFormatter::Json

This was surprising, because we only registered it once for one of two Grape root APIs that we have in our application:

class Root < ::API::RootAPI # inherits from Grape::API
  format :json

  error_representer ::Bim::Bcf::API::V2_1::Errors::ErrorRepresenter, "application/json; charset=utf-8"    
  # ...
end

Our intention was to only use this formatter in that one API, but keep the default formatter in other APIs.

After browsing the grape source code I found two surprises:

  1. Just by existing, our error formatter now registers itself globally, presumably as the default formatter for JSON errors
    • We only intended to use this error formatter in a comparably narrow scope, not everywhere
  2. The warning message I got was misleading, because it's the automatic registration of our formatter that caused the warning message already and jsonwas previously registered with Grape::ErrorFormatter::Json
    • The error lies in this line of code, where we should probably refer to registry[shortname] instead of klass, to say what was already registered

What's the intended way to define an error formatter that's only used in a small part of the API, but not the entire API?

References

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