Skip to content

[Feature Request] Raise a specific error class for missing view #569

@ryanmccarthypdx

Description

@ryanmccarthypdx

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe

A common pattern for using Blueprinter (at least at Procore) is to have the API consumers / clients request a specific blueprinter view via query params when making an http request. When the view they request does not exist, Blueprinter currently raises an error that looks like:

DEVELOPMENT(main)> WidgetBlueprint.render(Widget.last, view: :non_existent_view)
Blueprinter::BlueprinterError: View 'non_existent_view' is not defined

In this situation, we would want to raise a 400 and not a 500 for the end user. Blueprinter::BlueprinterError, however, is a very generic error and is used for things that could also be 500s.

Describe the feature you'd like to see implemented

I propose adding a new error class that inherits from Blueprinter::BlueprinterError for backwards compatibility purposes but which can be rescued separately and converted into 400s. Perhaps named Blueprinter::MissingViewError?

Describe alternatives you've considered

Currently, the workaround is to do something like this in the app:

      def blueprint_view
        return @blueprint_view if defined?(@blueprint_view)

        @blueprint_view = if DEFAULT_BLUEPRINT.view?(params[:serializer_view])
          params[:serializer_view]
        else
          self.class::DEFAULT_BLUEPRINT_VIEW
        end
      end

While #view? is a public method, I feel like the apps should not have to duplicate this effort that Blueprinter needs to do.

Additional context

No response

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