# Using try ... catch block in a job

**URL:** <https://community.openfn.org/t/using-try-catch-block-in-a-job/155>\
**Category:** Job Writing\
**Created:** [October 12, 2021, 3:19am UTC](https://community.openfn.org/t/using-try-catch-block-in-a-job/155 "2021-10-12T03:19:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![etdvlpr](https://community.openfn.org/user_avatar/community.openfn.org/etdvlpr/32/50_2.png) [@etdvlpr](https://community.openfn.org/u/etdvlpr)\
**Post date:** [October 12, 2021, 3:19am UTC](https://community.openfn.org/t/using-try-catch-block-in-a-job/155/1 "2021-10-12T03:19:07Z")

</div>

Hello there,

Can I use a try .. catch block with in a single job  
My scenario  
I’m using the http adaptor to try and add data to akeneo, which has variables set up as option lists

what I’m looking for is something like

```auto

get("www.example.com/1"); // header params
.
.

try {
    // check if variable exists
    fn(state => {
        var x = state.references[0].x.toLowerCase().trim();
        return get(`.../attributes/x/options/${x}`); // returns a 404 if it doesn't exist
    });
} catch (error) {
    post(`.../attributes/x/options`, {
        body: state => {
            return {
                "code": state.references[0].x.toLowerCase().trim(),
                "value": state.references[0].x
            };
        },
        header: state => {
            return {
                "y": state.references[1].y
            };
        },
    }); // create the option
}

```

Which I run for 8 more variables before finally adding the product with the variables set to their respective options

Problem is, state in the catch clause resets to initial state (empty references and data is one received in inbox)  
also, Is this the right approach to creating a job like this?

---

<div class="post-metadata">

**Author:** ![taylordowns2000](https://community.openfn.org/user_avatar/community.openfn.org/taylordowns2000/32/165_2.png) [@taylordowns2000](https://community.openfn.org/u/taylordowns2000)\
**Post date:** [October 12, 2021, 6:45am UTC](https://community.openfn.org/t/using-try-catch-block-in-a-job/155/2 "2021-10-12T06:45:26Z")

</div>

Very cool concept @etdvlpr !  
(cc: @Mamadou , @stu in case you have anything to add)

A few suggestions:

1. At the time of writing, you can’t use `try/catch` at the top level like that. Everything at the top level needs to be an [_operation_](https://docs.openfn.org/documentation/jobs/operations/). Also, as a general rule of thumb, your custom Javascript should go inside an `fn` operation.

2. Most of the functions that you’d commonly use at the top level will expect to be called with `state`… so if you use them inside a custom `fn` block, you’ll need to do so. For example, `return get('url');` should become `return get('url')(state)` if you want to execute it inside an `fn` block.

3. The “OpenFn way” of doing this would likely be with promises, making use of [`.then()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then), [`.catch()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch), and [`.finally()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally). Consider the code (with some of your data transformation commented out) below:

```js
get('https://jsonplaceholder.typicode.com/todos/1'); // header params

// check if variable exists
fn(state => {
  // var x = state.references[0].x.toLowerCase().trim();

  console.log('Trying to get some more data...');
  // returns a 404 if it doesn't exist
  return get(`https://jsonplaceholder.typicode.com/comments/501`)(state)
    .catch(error => {
      console.log('But failing with...', error.response.status);

      // create the option
      return post(`https://jsonplaceholder.typicode.com/comments`, {
        body: {
          // code: state.references[0].x.toLowerCase().trim(),
          // value: state.references[0].x,
        },
        // header: { y: state.references[1].y },
      })(state);
    })
    .then(result => result)
    .finally(result => result);
});

```

1. This is a small thing, but once you’re inside that `fn` block and making your `post` to “create the option” you can access parts of `state` directly, rather than through a function. When using these helper operations (like `get`, `post`, or `createPatient`) directly, OpenFn’s “magic” is knowing when to evaluate state. But inside a custom `fn` block you’re on your own… so if you define `state` on line 4, you’ll be plucking out state.references of _that state_ when you ask for it later in your code block.

This last point is definitely a subtle one, but the guiding principal is that inside an _open_ `fn` 😉 block, you’re writing code and doing exactly what you want… no magic (or interference) from [core](https://github.com/OpenFn/core).

---

<div class="post-metadata">

**Author:** ![etdvlpr](https://community.openfn.org/user_avatar/community.openfn.org/etdvlpr/32/50_2.png) [@etdvlpr](https://community.openfn.org/u/etdvlpr)\
**Post date:** [October 12, 2021, 7:03am UTC](https://community.openfn.org/t/using-try-catch-block-in-a-job/155/3 "2021-10-12T07:03:42Z")

</div>

I guess my java experience got the better of me.  
Thanks, it’s working brilliantly.
