From the course: Deep Dive Into Go Lang Interfaces

Unlock the full course today

Join today to access over 22,800 courses taught by industry experts or purchase this course individually.

Challenge: Custom error

Challenge: Custom error

From the course: Deep Dive Into Go Lang Interfaces

Start my 1-month free trial

Challenge: Custom error

(upbeat music) - [Instructor] For this challenge, I would like you to implement an error that contains also the call stack. So you need to define an error struct, and you need to think about what are the fields inside of it. You need to create a function called wrap. It's going to wrap an original error and add the call stack to that error. For your error to become an error, you need to implement the error interface. So this is the implementation, and I want you, that if your error is printed out with the plus V verb, it's going to print out the call stack. So you need to implement the FMT formatter interface for your error. I also included here the code to capture a call stack for a current location in the code. So you can use that in your code. You don't need to research that. Here's example that will make things clear. So first function is A, and we have wrap of an error, this just say oops. And then we have B, which calls A, and I'm create an error. I'm printing it using print...

Contents