Tips & tricks

How to debug?

  1. Is it really a bug?

  2. Can I find an error message?

    1. Yes! Does it ring a bell, or does it give some clues about the place in the code where the bug happens? There is often a line number, but you might need to scroll down/up to find it.

    2. No. That's probably not the case, did you look at the right place? If the error happened in the front-end, you need to check the Chrome console. If it happened in the back-end, you need to check your server logs (and/or your sidekiq logs if it's running, the error might come from there too!)

    3. No, I swear! Ok, then you need to isolate the issue at maximum. This becomes a game of finding some clue, so put your hat on and stop at every line of code of your flow if you need to.

  3. After you located the error, you need to understand how it happened, under what conditions it can occur and how you should fix it. Beware that your fix does not break something else, by making sure you truly understand what is happening. No rush here, this is the more important step. Go down that rabbit hole 🐰

  4. If you're lost, it's ok and it happens, no worries. You best friend for that is Stackoverflow, remember that thousands of people have probably been where you are now, so look for their solutions.

NB: sometimes, you'll get some error in the back-end while everything looks fine. Maybe the error spawns in the back-end because the front-end sends the wrong data? Go back on the flow, and make sure the front-end does its job correctly.

Common bugs

Address already in use

You try to run your server with foreman and boom, you get an "address already in use" kind of error. This probably means that some processes were not closed properly (e.g. your computer shutted down), and you'll need to manually kill them.

Basically, it means that you try to start a new server at a specific port and it tells you that this port is already used (while you know that your server ain't running).

To kill it manually, you'll first need to find the process ID (PID):

  1. Run lsof -i :8080 to find existing processes for the 8080 port

  2. When you have the right PID, kill it by running kill -9 <PID>

The -9 is just there to force your computer to shut it, just replace <PID> with the one you found and it should be good!

Here are the different ports that could cause issues:

  • 3000

  • 3035

  • 8080

DATABASE_URL

This env variable, if set, takes over the database.yml file. This file tells Rails which DB to use with which environment. For example, for the test environment (when you run Rspec), you expect Rails to run it with the test DB.

However, if the DATABASE_URL is set in your .env file, Rails won't look at the database.yml file but will use the value you referred in your .env.

With this variable set, Rails will use your dev DB even for the test environment. Your tests will fail by dozens and you'll be very confused!


The question now is: why would you ever want to have that variable in your local environment?

Short answer is: you need it for Forest Admin.

Basically, if you need to run Forest Admin locally, you'll need that variable. When you're done, don't forget to comment it (this way it stays in your file, but won't have any effect).

Process is not defined

You're missing an .env variable. Good thing is that you know how to debug, so you'll look the Chrome console and see the error, which will show you the exact line that failed ;)

See the above video for example: by inspecting we can quickly land on the right line, and we see that I currently miss the LANDING_BOT_CANDIDATE_EN key.

After adding the missing key, don't forget to restart your environments (server, rails console, Sidekiq) to make it available!

Good practice with env variables:

  • If you add a new variable, put it in the .env.example as well (this file is in the repo GitHub, .env is not for security reason. So in the .env.example replace the value with a placeholder to keep it safe). The .env.example permits to keep a track for env variables, very useful for the whole team

  • Again, if you add a new .env, don’t forget to add it for the review app, staging & prod env as well! In some cases, you’d also want to have a dedicated value for production .env variables (sometimes it’s not needed though) :> example with salt & pepper

Cannot connect on localhost

Can be many things, but often linked to your Postgres:

  • Try reinstalling it with the right version

  • Run « createdb » in a bash terminal (this should create a DB named after you on your machine)

  • If this command was not known, it means that Postgres installation failed

  • Run rails db:reset

  • Look on stack overflow, it may need some try & repeat

Searchkick Import - TOO MANY REQUESTS

This error occurs when your local server for Elasticsearch is full. To flush it, simply run this in a bash terminal:

This is a simple DELETE request to your local Elasticsearch server, which is hosted in the 9200 port.

Missing libraries

Don’t forget to run yarn & bundle to make sure you’re up to date.

Last updated