Skip to content

Instantly share code, notes, and snippets.

@ericpkatz
Last active April 3, 2021 15:04
Show Gist options
  • Save ericpkatz/ead6bf849a51c7da3d7b51ca7c6fd365 to your computer and use it in GitHub Desktop.
Save ericpkatz/ead6bf849a51c7da3d7b51ca7c6fd365 to your computer and use it in GitHub Desktop.

Revisions

  1. ericpkatz revised this gist Apr 3, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ syncAndSeed()
    .then( ()=> {
    console.log('seeded');//BUG this still prints because of swallowed error
    })
    .catch( ex => console.log(ex));//BUG there was an error
    .catch( ex => console.log(ex));//BUG there was an error but we don't catch it
    ```

    - note in the ***createByName*** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.
  2. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -31,14 +31,14 @@ const syncAndSeed = async()=> {
    };
    ```

    - demonstration of the bug is shown in the code below.
    - demonstration of a bug is shown in the code below.

    ```
    ```javascript
    syncAndSeed()
    .then( ()=> {
    console.log('seeded');//BUG this still prints because of swallowed error
    })
    .catch( ex => console.log(ex));
    .catch( ex => console.log(ex));//BUG there was an error
    ```

    - note in the ***createByName*** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.
  3. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error
    - both of these often result in difficult to detect bugs

    - I think this is a valid example with Sequelize. You can imagine the **createByName** method being called in an express route, but I'm just using it to seed the database.
    - I think this is a valid example with Sequelize. You can imagine the ***createByName*** method being called in an express route, but I'm just using it to seed the database.

    ```javascript
    const Sequelize = require('sequelize');
    @@ -41,9 +41,9 @@ syncAndSeed()
    .catch( ex => console.log(ex));
    ```

    - note in the **createByName** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.
    - note in the ***createByName*** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.

    - the solution is to **catch the error and throw it** or to **not catch it at all**
    - the solution is to ***catch the error and throw it*** or to ***not catch it at all***

    ```javascript
    User.createByName = async function(name){
  4. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error
    - both of these often result in difficult to detect bugs

    - I think this is a valid example with Sequelize. You can imagine the createByName method being called in an express route, but I'm just using it to seed the database.
    - I think this is a valid example with Sequelize. You can imagine the **createByName** method being called in an express route, but I'm just using it to seed the database.

    ```javascript
    const Sequelize = require('sequelize');
    @@ -29,7 +29,11 @@ const syncAndSeed = async()=> {
    await User.createByName('moe');
    await User.createByName('larry');//this won't work, max 4 characters
    };
    ```

    - demonstration of the bug is shown in the code below.

    ```
    syncAndSeed()
    .then( ()=> {
    console.log('seeded');//BUG this still prints because of swallowed error
  5. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    - a swallowed error in a thunk will prevent a component from detecting the error
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error
    - both of these often result in difficult to detect bugs

    - I think this is a valid example with Sequelize. You can imagine the createByName method being called in an express route, but I'm just using it to seed the database.

  6. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ syncAndSeed()

    - note in the **createByName** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.

    - the solution is to **catch the error then throw it** or to **not catch it at all**
    - the solution is to **catch the error and throw it** or to **not catch it at all**

    ```javascript
    User.createByName = async function(name){
  7. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ syncAndSeed()

    - note in the **createByName** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.

    - the solution is to **not catch the error** if you are going to swallow it (no catch) or **catch it but then throw it**
    - the solution is to **catch the error then throw it** or to **not catch it at all**

    ```javascript
    User.createByName = async function(name){
  8. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -36,9 +36,9 @@ syncAndSeed()
    .catch( ex => console.log(ex));
    ```

    - note in the createByName method I am swallowing the error. If you see code which catches an error and doing nothing with it, then it is swallowing the error. Usually not a good sign.
    - note in the **createByName** method I am swallowing the error. If you see code which catches an error, and does nothing with it, then it is swallowing the error. Usually not a good sign.

    - the solution is to not catch the error if you are going to swallow it (no catch) or catch it but then throw it
    - the solution is to **not catch the error** if you are going to swallow it (no catch) or **catch it but then throw it**

    ```javascript
    User.createByName = async function(name){
  9. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    - there is something that I've seen in some of our workshops and solutions that I think are a bit problematic, which I wanted to address.
    - basically it's the "swallowing" of errors. I see the issue in Sequelize models and in thunks
    # Issue - the "swallowing" of errors

    - a swallowed error in a thunk will prevent a component from detecting the error
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error

  10. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    - a swallowed error in a thunk will prevent a component from detecting the error
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error

    - I think this is a valid example
    - I think this is a valid example with Sequelize. You can imagine the createByName method being called in an express route, but I'm just using it to seed the database.

    ```javascript
    const Sequelize = require('sequelize');
  11. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    - there is something that I've seen in some of our workshops and solutions that I think are a bit problematic which I wanted to address.
    - there is something that I've seen in some of our workshops and solutions that I think are a bit problematic, which I wanted to address.
    - basically it's the "swallowing" of errors. I see the issue in Sequelize models and in thunks
    - a swallowed error in a thunk will prevent a component from detecting the error
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error
    @@ -36,7 +36,7 @@ syncAndSeed()
    .catch( ex => console.log(ex));
    ```

    - note in the createByName method I am swallowing the error, if you see code which catches an error and doing nothing with it, then it is swallowing the error.
    - note in the createByName method I am swallowing the error. If you see code which catches an error and doing nothing with it, then it is swallowing the error. Usually not a good sign.

    - the solution is to not catch the error if you are going to swallow it (no catch) or catch it but then throw it

  12. ericpkatz revised this gist Mar 31, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    - a swallowed error in a thunk will prevent a component from detecting the error
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error

    - I think this is an example
    - I think this is a valid example

    ```javascript
    const Sequelize = require('sequelize');
    @@ -26,12 +26,12 @@ User.createByName = async function(name){
    const syncAndSeed = async()=> {
    await conn.sync({ force: true });
    await User.createByName('moe');
    await User.createByName('larry');
    await User.createByName('larry');//this won't work, max 4 characters
    };

    syncAndSeed()
    .then( ()=> {
    console.log('seeded');
    console.log('seeded');//BUG this still prints because of swallowed error
    })
    .catch( ex => console.log(ex));
    ```
  13. ericpkatz created this gist Mar 31, 2021.
    61 changes: 61 additions & 0 deletions swallowing_errors.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    - there is something that I've seen in some of our workshops and solutions that I think are a bit problematic which I wanted to address.
    - basically it's the "swallowing" of errors. I see the issue in Sequelize models and in thunks
    - a swallowed error in a thunk will prevent a component from detecting the error
    - a swallowed error in a Sequelize method will prevent an express route from detecting the error

    - I think this is an example

    ```javascript
    const Sequelize = require('sequelize');
    const { DataTypes: { STRING } } = Sequelize;
    const conn = new Sequelize(process.env.DATABASE_URL || 'postgres://localhost/acme_db');

    const User = conn.define('user', {
    name: STRING(4)
    });

    User.createByName = async function(name){
    try {
    return await this.create({ name });
    }
    catch(ex){
    console.log(ex);
    }
    };

    const syncAndSeed = async()=> {
    await conn.sync({ force: true });
    await User.createByName('moe');
    await User.createByName('larry');
    };

    syncAndSeed()
    .then( ()=> {
    console.log('seeded');
    })
    .catch( ex => console.log(ex));
    ```

    - note in the createByName method I am swallowing the error, if you see code which catches an error and doing nothing with it, then it is swallowing the error.

    - the solution is to not catch the error if you are going to swallow it (no catch) or catch it but then throw it

    ```javascript
    User.createByName = async function(name){
    try {
    return await this.create({ name });
    }
    catch(ex){
    console.log(ex);
    throw ex;
    }
    };
    ```

    or even better

    ```javascript
    User.createByName = function(name){
    return this.create({ name });
    };
    ```