# Catching errors in environment variable type coercion

**URL:** <https://discourse.dry-rb.org/t/catching-errors-in-environment-variable-type-coercion/537>\
**Category:** Support\
**Created:** [April 18, 2018, 11:55pm UTC](https://discourse.dry-rb.org/t/catching-errors-in-environment-variable-type-coercion/537 "2018-04-18T23:55:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmaze](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dmaze](https://discourse.dry-rb.org/u/dmaze)\
**Post date:** [April 18, 2018, 11:55pm UTC](https://discourse.dry-rb.org/t/catching-errors-in-environment-variable-type-coercion/537/1 "2018-04-18T23:55:10Z")

</div>

[https://gist.github.com/dmaze/308034415354fb0ec2e537157b3a39b8](https://gist.github.com/dmaze/308034415354fb0ec2e537157b3a39b8) is a minimal dry-system script that reads an integer value from an environment variable (using the `:settings` feature). If I run the script like

```auto
AN_INT=10 ruby ./dry-rb-env.rb

```

it will print out the number like I expect. But if I either

```auto
AN_INT=foo ruby ./dry-rb-env.rb
unset AN_INT; ruby ./dry-rb-env.rb

```

I get rather long backtraces that don’t mention the variable name at all.

Three questions:

(1) Is there a good way to trap these errors and give a succinct error message (something like `AN_INT: invalid value for Integer(): "foo"` would be great)?

(2) When I do report that, can I distinguish “missing” from “invalid”? (`AN_INT is required`, not `AN_INT: can't convert nil into Integer`)

(3) The value type has a `.default(17)` value, so shouldn’t that get picked up?

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [April 30, 2018, 11:35pm UTC](https://discourse.dry-rb.org/t/catching-errors-in-environment-variable-type-coercion/537/2 "2018-04-30T23:35:56Z")

</div>

@dmaze These feel like legitimate bugs to me, would appreciate it if you could file issues on dry-system’s GitHub repo! PRs are welcome too 😉
