Elixir Try Catch -
i trying figure out scoping issue. have procedural function setting bunch of related resources on aws. need able catch failure , rollback relations have been setup. have try catch setup, variables in try block not available in catch, need them can take correct steps rollback.
try c = connection cert = aws.cert module = aws.create_mod(cert) etc... rescue :error -> rollback(c, cert, module) end
any advice how handle this?
this happens because elixir cannot guarantee variables set time exception raised. may not case you, imagine this.
try foo = do_something_safe() bar = do_something_that_will_raise_an_error() baz = do_something_else_safe() ... rescue runtimeerror -> quux(foo, baz) end
in previous example, call do_something_that_will_raise_an_error()
raise error. both bar
, baz
not set because of it.
in specific case, may able like
baz = do_something_else_safe() foo = do_something_safe() try bar = do_something_that_will_raise_an_error() ... rescue runtimeerror -> quux(foo, baz) end
now, if call do_something_that_will_raise_an_error()
raises error, still have foo
, , baz
variables set, , can used in rescue block.
basically, setup variables can outside of try
. this gives nice little overview of variable scope inside of try ... rescue
.
with in mind, might better setup supervisor , inside of genserver
(or other supervised process really). way, if crashes, supervisor can decide it. both elixir , erlang of mindset "let crash" instead of trying program defensively.
Comments
Post a Comment