Wrapping errors

Report a typo

Mr. Krabs bought a used self-serve ice cream machine for the Krusty Krab that serves ice cream through a Go program... After just one day of use, the ice cream machine broke down, just like in every other fast food restaurant!

As usual, Mr. Krabs doesn't want to spend any money on a "broken ice cream machine" sign. Please help him create the main error message and a wrapped error message within the brokenMessage function with the following contents:

  • main error: "ice cream machine is broken"
  • wrapped error: "error: can't serve you an ice cream main cause of error:"

Take notice that the error message will automatically show up when customers try to use the machine and input the flavor of the ice cream they want:

error: can't serve you an ice cream main cause of error: ice cream machine is broken
Write a program in Go
package main

import (
"errors"
"fmt"
)

func brokenMessage() error {
// create the main error here - you can use errors.New or fmt.Errorf
err := ?

// create the wrapped error here using fmt.Errorf and the %w verb
wrappedErr := ?

return wrappedErr // Do not delete this line!
}

// DO NOT delete or modify the contents of the main function!
func main() {
if err := brokenMessage(); err != nil {
fmt.Println(err)
}
}
___

Create a free account to access the full topic