Exception: BetterService::Errors::Runtime::ResourceNotFoundError

Inherits:
RuntimeError show all
Defined in:
lib/better_service/errors/runtime/resource_not_found_error.rb

Overview

Raised when a required resource is not found

This error wraps ActiveRecord::RecordNotFound exceptions raised during service execution (usually in the search phase).

Examples:

Resource not found

class UserShowService < BetterService::Services::ShowService
  model_class User

  schema do
    required(:id).filled(:integer)
  end
end

UserShowService.new(user, params: { id: 99999 }).call
# => raises ResourceNotFoundError wrapping ActiveRecord::RecordNotFound

In custom service

class MyService < BetterService::Services::Base
  schema { required(:user_id).filled(:integer) }

  search_with do
    User.find(params[:user_id])  # Raises RecordNotFound if not exists
  end
end

MyService.new(user, params: { user_id: 99999 }).call
# => raises ResourceNotFoundError

Instance Attribute Summary

Attributes inherited from BetterServiceError

#code, #context, #original_error, #timestamp

Instance Method Summary collapse

Methods inherited from BetterServiceError

#backtrace, #detailed_message, #inspect, #to_h

Constructor Details

#initialize(message = "Resource not found", code: :resource_not_found, context: {}, original_error: nil) ⇒ ResourceNotFoundError

Returns a new instance of ResourceNotFoundError.



35
36
37
# File 'lib/better_service/errors/runtime/resource_not_found_error.rb', line 35

def initialize(message = "Resource not found", code: :resource_not_found, context: {}, original_error: nil)
  super(message, code: code, context: context, original_error: original_error)
end