Git Product home page Git Product logo

phase-3-reading-error-messages's Introduction

Reading Ruby Error Messages

Learning Goals

  • Read the different parts of an error message
  • Identify common types of errors

Introduction

In this lab, you'll be reading error messages from tests. This lab is designed so that both running the files and running the test suite via the learn test command will show the error messages for you to decode. Moving forward though, you'll be reading error messages mainly through running the test suite.

Reading Error Messages

Let's start by running some of the Ruby code in the lib folder to produce an error message. Run this in your terminal:

$ ruby lib/a_name_error.rb

Error messages have 3 parts:

lib/a_name_error.rb:3:in `<main>': undefined local variable or method `hello_world' for main:Object (NameError)
  1. The location of the error, the "where":

    lib/a_name_error.rb:3:in `<main>':
    • lib/a_name_error.rb is the file the error occurred in.
    • 3 is the line of code with the error.
    • <main> is the scope of the error.
  2. The description, the "why":

    undefined local variable or method `hello_world' for main:Object

    The interpreter does the best job it can to tell you what it thinks went wrong.

  3. The type of error, the "who":

    (NameError)

    This is a Ruby Error Type.

You've solved games of Clue with less information. This is one of the best parts of programming: debugging and fixing errors. It's like you're a detective solving a crime. The only bad thing is that more often than not, you're also the criminal that caused the error in the first place.

Errors are clues that the interpreter is giving you. They point you toward how to fix the program and move on.

Four Common Error Types

Name Errors

Name errors are caused when a given name is invalid or undefined. Whenever the Ruby interpreter encounters a word it doesn't recognize, it assumes that word is the name of a variable or a method. If that word was never defined as either a variable or a method, it will result in a name error. Try this out in IRB:

$  irb
2.7.3 :001 > a_variable
NameError (undefined local variable or method `a_variable' for main:Object)
2.7.3 :002 > a_variable = 7
 => 7
2.7.3 :003 > a_variable
 => 7

Trying to access a_variable before assigning it a value results in a NameError, which we can fix by assigning it some value.

Syntax Errors

Syntax errors are pretty self-explanatory: they're the result of incorrect syntax. Thankfully, they're usually followed by a guess about the location of the error. For example, imagine we had included the following code inside some Ruby code:

2.times do
  puts "hi"

Running our code would result in:

2: syntax error, unexpected end-of-input, expecting keyword_end

Here, Ruby is saying that on line 2, there is a missing end (every do keyword must be followed by some code and then an end keyword). Always read the full details of syntax errors and look for line numbers, which usually appear at the beginning of the error message.

Note: You won't be able to reproduce the above syntax error using IRB, because IRB won't execute the code until you enter a matching end keyword for the do block. You can see this error by creating a Ruby file, adding the code above, and executing the file.

Type Errors

When you try and do a mathematical operation on two objects of a different type, you will receive a TypeError. For example if you try and add a string to an integer, Ruby will complain.

1 + "1"

Will produce the following error:

TypeError: String can't be coerced into Integer

Division Errors

A DivisionError is caused when a given number is divided by 0.

Instructions

To get started, run learn test --f-f to run the first test in the test suite. Use the error messages to guide your work:

  • Read the errors. Scroll through the entire output to get a sense of what the failures are trying to tell you. What does the error mean? How can we fix it?

  • Each error prints out a stack trace, which points to where the code failed and attempts to follow it up the stack โ€” that is, through the bits of code that ran leading up to the failure. You can use these stack traces to pinpoint which line(s) of code need your attention.

  • These stack traces can also point you to which files you should run to get a better sense of the errors.

Fix the errors in each of the files in lib/. Then confirm the fix by running learn test again.

phase-3-reading-error-messages's People

Contributors

aemrich avatar ahimmelstoss avatar annjohn avatar aviflombaum avatar babyshoes avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar gj avatar ihollander avatar ivalentine avatar joll59 avatar jonbf avatar justronnie01 avatar lizbur10 avatar loganhasson avatar markedwardmurray avatar maxwellbenton avatar obiemunoz avatar pletcher avatar sammarcus avatar sarogers avatar simplificated avatar sophiedebenedetto avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-3-reading-error-messages's Issues

"Errors are clues" sentence/phrasing is complex

Canvas Link

https://learning.flatironschool.com/courses/5187/assignments/180455?module_item_id=397714

Concern

Readers can comprehend the meaning of this, but the extra mental processing isn't worth it:
Errors are clues, and reading them is the interpreter telling you what to do to fix the program and move on.

Additional Context

No response

Suggested Changes

Maybe change this:
Errors are clues, and reading them is the interpreter telling you what to do to fix the program and move on.

to this?:
Error messages are clues. Reading them lets the interpreter guide you in fixing the program and moving on.

sample error message shows "Fixnum" but my screen shows "Integer"

Canvas Link

https://learning.flatironschool.com/courses/5187/assignments/180455?module_item_id=397714

Concern

For what it's worth, I don't see this error message
TypeError: String can't be coerced into Fixnum

but instead see:
TypeError: String can't be coerced into Integer

Additional Context

No response

Suggested Changes

Maybe other people see "Fixnum" so the message still matches for them.

For me when I see "Integer" on the screen, I think "Flatiron's stuff isn't keeping up."

? Maybe change to:
TypeError: String can't be coerced into Integer

Clarification For Ruby Script Portion Of "Reading Ruby Error Messages" Lesson

Canvas Link

https://learning.flatironschool.com/courses/5286/assignments/172778?module_item_id=376608

Concern

It would be easier to just flat out mention that the following section is meant to be ran in a related Ruby script:
This is the related section I am referring to:

For instance:

2.times do
  puts "hi"

Additional Context

No response

Suggested Changes

This should be changed for better clarification:

For instance, if we create a Ruby script that contains the following code snippet:

2.times do
  puts "hi"

If we later run this same Ruby script above, this will result in this error:

2: syntax error, unexpected end-of-input, expecting keyword_end

document's numbers each of 3 error messages as "1."

Canvas Link

https://learning.flatironschool.com/courses/5187/assignments/180455?module_item_id=397714

Concern

in this text:
Error messages have 3 parts:

lib/a_name_error.rb:3:in <main>': undefined local variable or method hello_world' for main:Object (NameError)

  1. The location of the error, the "where":
    .
    .
    .

  2. The description, the "why":
    .
    .

  3. The type of error, the "who":

Additional Context

No response

Suggested Changes

change formatter so that this:

  1. The description, the "why":
    .
    .

  2. The type of error, the "who":

appears as:
2. The description, the "why":
.
.

  1. The type of error, the "who":

Hardcode these? Use bullets? It seems that the numbering starts fresh with "1." for each issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.