The short answer

Read an error message in this order: what went wrong (the message), where (the file and line number), and how you got there (the stack trace, read from the top until you reach your own code). Then change one thing, run again, and read the new message. Most bugs are found in minutes this way.

Ewoi

Errors are not the computer shouting at you. They are the computer telling you exactly where it got confused. Treat every error as a clue.

Why beginners struggle with errors

An error message looks like a wall of scary text, so the natural reaction is to panic, scroll past it, and start changing random things. That makes debugging slow and frustrating.

Developers are not smarter at this; they have a habit. They slow down and read the error like a note left for them.

The three parts of almost every error

Here is a typical PHP error from a Laravel app:

ErrorException: Undefined variable $total
in /var/www/app/Http/Controllers/OrderController.php:42
Part Here What it tells you
Type ErrorException The kind of problem
Message Undefined variable $total What went wrong, in plain-ish words
Location OrderController.php:42 The file and line where it was noticed

So this says: "On line 42 of OrderController.php, you used a variable called $total that was never given a value." Open that file, go to line 42, and look for where $total should have been set. Often it is a typo ($totl) or it is set inside an if that did not run.

Reading a stack trace

Sometimes the error happens deep inside a library, not in your code. The stack trace is the list of steps (function calls) that led there, like footprints. Here is a JavaScript example from a browser console:

TypeError: Cannot read properties of undefined (reading 'name')
    at showUser (app.js:12:24)
    at loadPage (app.js:30:5)
    at main.js:4:1

Read it like this:

  1. Top line: the type and message. "You tried to read name from something that is undefined", meaning a value that does not exist yet.
  2. Next lines: where it happened, most recent first. It broke in showUser, on line 12, column 24 of app.js.
  3. Further down: how you got there. showUser was called by loadPage, which was called from main.js.

The rule: read from the top down, and stop at the first line that is in your own code. Lines inside frameworks or libraries are usually not where the bug is; they are where the bad value finally caused trouble.

Try itIn the error above, the bug is probably that showUser was called with no user, perhaps because the data had not loaded yet. Line 30 (loadPage) is where to check what was passed in.

Common error types and what they usually mean

Error says Usually means First thing to check
undefined, null, Undefined variable A value you expected does not exist Where the value should have been set
syntax error, unexpected ... A typo in the code's grammar The line shown and the line just above: a missing ;, ) or }
not found, does not exist, No such file A name or path is wrong Spelling, capital letters, the folder you are in
Permission denied The user running the code may not access a file File permissions (see Linux files and permissions)
Connection refused, could not connect A service is not running or the address is wrong Is the database or server running? Is the host and port right?
Status code 404 No page or route matched The URL and your routes
Status code 500 The server's code crashed The server's error log for the real message
Status code 419 (Laravel) The form's security token expired or is missing @csrf in the form, or a page left open too long

When the browser only says "500"

A 500 page hides the real error from visitors, which is correct for security. The real message is in your logs:

  • Laravel: storage/logs/laravel.log. Show the newest lines with tail -n 50 storage/logs/laravel.log.
  • Your browser: open developer tools, the Console tab for JavaScript errors and the Network tab for failed requests.

On your own computer, Laravel shows a detailed error page when APP_DEBUG=true in .env. On a live server, it must be false, because the detailed page can reveal secrets to anyone.

A debugging routine you can trust

  1. Read the whole message. Out loud if it helps.
  2. Find the first line in your own code in the stack trace.
  3. Form a guess: "I think $total is never set when the cart is empty."
  4. Test the guess with one change, or by printing the value just before the line (dd($total) in Laravel, console.log(user) in JavaScript).
  5. Run again and read the new message. A different error is progress.
  6. When it works, understand why before moving on.
Ewoi

Change one thing at a time. If you change five things and it works, you will not know which one fixed it, and the bug will come back.

Searching an error well

Copy the message, but remove what is unique to your computer (your file paths, your variable names, IDs). Add the tool and version.

  • Weak search: error line 42 /home/amina/shop/app/OrderController.php
  • Strong search: Laravel 13 "Undefined variable" in controller

Prefer answers from the official documentation and recent, highly voted forum answers. Check the date: an answer from many versions ago may no longer apply.

Ask your AI assistant

I am a beginner. Here is my full error message and the code around the line it mentions: [paste both]. Explain in simple words what the error means, which line is most likely the real cause, and one thing I can print to confirm it. Do not rewrite my whole file.

Key takeaways

  • Every error tells you what, where and how you got there.
  • In a stack trace, read from the top and stop at the first line in your own code.
  • A 500 page hides the details; the real message is in the logs.
  • Change one thing at a time, and treat a new error as progress.
  • Search the message without your personal paths, plus the tool and version.

Check your understanding

1. In a stack trace, where should you usually start looking for the bug?

The top shows where it broke; framework lines are usually where a bad value from your code caused trouble.

2. Your site shows "500 Server Error". Where is the real message?

500 pages hide details from visitors on purpose. The log has the full message and the line.

3. You changed three things and the bug disappeared. What is the problem?

Change one thing at a time so you understand the cause, and keep the fix you can explain.

Found this useful?

Every lesson stays free. Tap the thumbs up so we know what works, or say thanks with a soda or a coffee: it pays for hosting and new lessons.

Buy me a sodaBuy me a coffee

Related

WhatsApp

Join the channelChat with us

Free lessons and updates in the channel. Questions about learning or training? Chat with us.