Handling Errors

Learn how to handle errors that occur during the add to cart process.

It’s apparent from the page shown in the application below that our application raised an exception of the carts controller.

An example to demonstrate the error

Let’s run this application and try to add a product to the cart using the “Add to Cart” button. We will probably see the error.

# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
#   git config --global core.excludesfile '~/.gitignore_global'

# START:files
# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
/db/*.sqlite3-*

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore uploaded files in development.
/storage/*
!/storage/.keep

/public/assets
.byebug_history

# Ignore master key for decrypting credentials and more.
/config/master.key

/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity
# END:files
A running application to check the cart error

If the cart can’t be found, Active Record raises a RecordNotFound exception, which we clearly need to handle. The question, then, is how?

We could simply ignore it. From a security standpoint, this is probably the best move, because it gives no information to a potential attacker. However, it also means that if we ever have a bug in our code that generates bad cart IDs, our application will appear to ...