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

Unexpected error when default value throws an error #46

Closed
matthewmcgarvey opened this issue May 15, 2020 · 2 comments · Fixed by #51
Closed

Unexpected error when default value throws an error #46

matthewmcgarvey opened this issue May 15, 2020 · 2 comments · Fixed by #51

Comments

@matthewmcgarvey
Copy link
Member

matthewmcgarvey commented May 15, 2020

Say you have a default setting that can fail occasionally (in this example it fails every time but you get the point):

class MyClass
  Habitat.create do
    setting my_setting : String = raise "OOPS!"
  end
end

When you call MyClass.new, you'd expect the error to raise that comes from the default argument. Instead, you get:

Recursion while initializing class variables and/or constants (Exception)
         from /usr/local/Cellar/crystal/0.34.0/src/crystal/once.cr:21:9 in 'once'
         from /usr/local/Cellar/crystal/0.34.0/src/crystal/once.cr:48:3 in '__crystal_once'
         from ~MyClass::Settings::my_setting:read
         from spec/habitat_spec.cr:51:3 in 'broken_setting?'
         from src/habitat.cr:29:3 in 'raise_if_missing_settings!'
         from spec/habitat_spec.cr:151:5 in '->'
         from /usr/local/Cellar/crystal/0.34.0/src/spec/example.cr:255:3 in 'internal_run'
         from /usr/local/Cellar/crystal/0.34.0/src/spec/example.cr:35:16 in 'run'
         from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:296:23 in 'internal_run'
         from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:291:7 in 'run'
         from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:48:23 in 'run'
         from /usr/local/Cellar/crystal/0.34.0/src/spec/dsl.cr:270:7 in '->'
         from /usr/local/Cellar/crystal/0.34.0/src/crystal/at_exit_handlers.cr:255:3 in 'run'
         from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:45:14 in 'main'
         from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:114:3 in 'main'
@jwoertink
Copy link
Member

Alright, so looking in to this, I noticed a different issue in Crystal reported here.

To solve this for now, we can catch if it raises, but then what happens is instead of getting your "OOPS!", you'll get

The 'my_setting' setting for MyClass was nil, but the setting is required

That's probably ok for the time being, and at least a lot more helpful. What do you think @matthewmcgarvey ?

@matthewmcgarvey
Copy link
Member Author

I like that 👍

jwoertink added a commit that referenced this issue Jun 23, 2020
… an exception. We catch that and return nil so Habitat will throw the missing setting error. Fixes #46
jwoertink added a commit that referenced this issue Jun 25, 2020
Catch when a default value raises an exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants