Lesson Description

The "Debugging" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to debug and fix errors in a Go backend application. He identifies an internal error in the handler and traces it back to a problem in the movie repository. He also implements a handler for random movies using similar logic.

Preview
Close

Transcript from the "Debugging" Lesson

[00:00:00]
>> Maximiliano Firtman: Will it work? Well, we'll see. If I open the terminal, at least it says serving the files, which means there was no compilation errors. Remember, if we do have a compilation error, no, I have this if I save what will happen is that I will see the error here, why?

[00:00:20]
Because this is air, air is taking the change and it's not gonna compile. So if it's working a save, you will see building, running, serving device, so the server is there. So I'm going to the browser and trying our API movie stop. And I'm getting 404 and you say, why I'm getting 404?

[00:00:46]
This is something that we have discussed before. The browser out of the trailing slash, and that was cached because I used that before. That was caching my browser. We say, okay, let's remove that, but if I remove it, it appears again because that was cached by the browser.

[00:01:03]
I need to clear the cache on my browser to stop doing that, okay? So this is typically a problem when you have those slashes of course. You can also change the URL. So here, try this, okay? Just to see if it's working before clearing the cache. And if I refresh, I'm getting an internal error and a null.

[00:01:26]
So what to do? Well, first, if you look at the terminal, I have nothing. So now I need to debug and see where do I have a problem. So, who is sending that internal error? Let's try to debug. The handler. The handler. So, we go to the handler, and it should be here, right?

[00:01:55]
So this one is sending an internal error. Let's say Internal Error Getting Movies. If I save this, restart, you can see the message is coming from there. So the handler is calling storage. So, somehow, we need to go now to the storage. So we go to movie repository to get top movies.

[00:02:24]
Actually get top movies is just the query, and it's calling get movies that is the one that is making the query, the final query, okay? So this is the one that is trying to make the query, and failing somehow. So we know that the problem is here, okay?

[00:02:43]
Somewhere, well, actually the problem is that the query right now has only one argument $1. But when we were playing answering a question I added the second argument, remember this is the query and after the query we have n possible arguments, and they should match the same amount of arguments that you set in the query.

[00:03:04]
So actually, if I remove that 10 and I go back here and refresh, cross your fingers, I do have the list of 20 top movies. And what about random? If I try random, do I have the handler? Well, let's see first. So let's go, let's see. I have main.go, main.go have random.

[00:03:32]
Well, let's keep the same standard for the URLs with the training slash. So API movies random is set in a handler at GetRandomMovies. GetRandomMovies is from here, that is still the old one, okay? Dummy data. So I should remove this, and I should do something similar from the previous one.

[00:04:00]
So I will copy and paste, don't do that at home, and then I will get the random movies now. And this is GetRandomMovies, I think, okay, and this is Get Random Movies Error. So if everything works, if I go now to random, I'm getting fear awake as the first one.

[00:04:24]
If I refresh, now I'm getting Red Cliff, and every time, I'm getting a new list of 20 movies that are coming from the server. So, let's review where we are now. So we are the client now. So we have from HTTP, we are sending a request to forward slash API, forward slash movies, forward slash random, or top.

[00:04:54]
The backend in Go is actually creating everything here, so it's picking a handler. Is not the handler for the file system. It's actually a handler that is managing, in this case, random or top movies, receives a model repository. The model repository goes and creates a query. So SQL came back as rows.

[00:05:22]
The model repository converts that into a slice of our model of movies. So an array of movies. So it's sending the array of movies back to the handler. And the handler is finally converting that array of movies into JSON and sending that back through HTTP to, in this case the browser, okay?

[00:05:47]
So we have the backend part working, of course, there are still some missing services, but we have at least two services that are working, okay? In the backend, side.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now