Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more ruby like rescues method #77

Closed
catmando opened this issue Dec 6, 2018 · 1 comment
Closed

add more ruby like rescues method #77

catmando opened this issue Dec 6, 2018 · 1 comment
Labels
enhancement New feature or request needs doc Everything is working, but documentation is needed. ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Milestone

Comments

@catmando
Copy link
Contributor

catmando commented Dec 6, 2018

The after_error hook (called error boundaries in React) is quite primitive.

But we can use it to build a more ruby like rescues hook:

class MyComponent  
  ...
  rescues do |error, info|
    ...  # do whatever here
  end
end

Like ruby rescue, the rescues method can take a list of exception classes. If the error raised is an instance of one of the exception classes, the rescues block will catch the error, otherwise it will get passed down the stack to any components further up (down?) the tree.

Like ruby rescue you can have several rescues in a component each with different blocks, or you can share the block with multiple exception classes.

Like ruby rescue if given no exception class rescues will default to StandardError

Also rescues will automatically create the necessary wrapper classes, so that you can rescue errors raised in the component you are defining. This is not possible with after_error which can only catch errors raised in components mounted from within the component defining the boundary.

class App < HyperComponent
  include Hyperstack::Router

  SCOPES = %i[all active completed]
  VALID_ROUTES = "/:scope(#{SCOPES.join('|')})"

  class NoRouteMatched < StandardError
  end

  render(SECTION, class: 'todo-app') do
    DIV { @flash_message }
    if @error_message
      DIV do
        DIV { @error_message }
        BUTTON { 'okay' }.on(:click) { mutate @error_message = nil }
      end
    else
      Header()
      Switch() do
        Route(App::VALID_ROUTES) { |match| Index(scope: Todo.send(match.params[:scope])) }
        Route('/', exact: true) { Redirect('/all') }
        Route('*') { |match| raise NoRouteMatched, match.url }
      end
      Footer()
    end
  end

  rescues NoRouteMatched do |err|
    mutate @flash_message = "I did not recogonize #{err.message}", history.push('/all')
    after(3) { mutate @flash_message = nil }
  end

  rescues do |err|
    mutate @error_message = "Something went wrong! #{err}"
  end
end
@catmando catmando added the enhancement New feature or request label Dec 6, 2018
@catmando catmando added this to the alpha1.3 milestone Dec 7, 2018
@catmando
Copy link
Contributor Author

catmando commented Jan 15, 2019

fixed. For example see hyper-component/client_features/rescues_spec.rb

@catmando catmando added ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch needs doc Everything is working, but documentation is needed. and removed ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch labels Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs doc Everything is working, but documentation is needed. ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Projects
None yet
Development

No branches or pull requests

1 participant