From the course: Deep Dive Into Go Lang Interfaces
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Custom error
From the course: Deep Dive Into Go Lang Interfaces
Solution: Custom error
(upbeat music) - [Instructor] Let's have a look at my solution. So here's my Error struct. I have the cause which is the original error, and I have the stack which is going to be just a string 'cause I just need it for printing up. Next, I have the Wrap function. If the error is nil, I'm going to return nil. So there are no errors here. Otherwise, I'm using bytes.Buffer as an in-memory writer, and I'm printing the error with a new line after that. And then I'm printing the callStack. And then I'm creating the error with the cause which is the original error and the stack which is what's inside this bytes.Buffer. And here is the callStack from the beginning. The Error which implements the error interface just returned the cause, Error. So this is how it's printed out. And the Format which implements fmt.Format checks if the verb is v and we have a plus sign, I'm going to print out the stack. Otherwise, I'm just going to print out the error. Now, if we go to our example test, and we're…