erratum

package
v0.0.0-...-543a44c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2018 License: GPL-3.0 Imports: 2 Imported by: 0

README

Error Handling

Implement various kinds of error handling and resource management.

An important point of programming is how to handle errors and close resources even if errors occur.

If you are new to Go errors or panics we recommend reading the documentation on these topics first for context.

In this exercise you will be required to define a function Use(o ResourceOpener, input string) error that opens a resource, calls Frob(input) on the result resource and then closes that resource (in all cases). Your function should properly handle errors and panics.

ResourceOpener o will be a function you may invoke directly o() in an attempt to "open" the resource. It returns a Resource and error value in the idiomatic Go fashion:

See the common.go file for the definitions of Resource, ResourceOpener, FrobError and TransientError. You will define your solution to be in the same package as common.go and error_handling_test.go: "erratum". This will make those types available for use in your solution.

There will be a few places in your Use function where errors may occur:

  • Invoking the ResourceOpener function passed into Use as the first parameter, it may fail with an error of type TransientError, if so keep trying to open the resource. If it is some other sort of error, return it from your Use function.

  • Calling the Frob function on the Resource returned from the ResourceOpener function, it may panic with a FrobError (or another type of error). If it is indeed a FrobError you will have to call the Resource's Defrob function using the panic FrobError's .defrobTag variable as input to the Defrob function. Either way Use should return the error.

  • Also note: if the Resource was opened successfully make sure to call its Close function no matter what (even if errors have occurred).

Testing for specific error types may be performed by type assertions. You may also need to look at named return values as a helpful way to return error information from panic recovery.

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Use

func Use(o ResourceOpener, input string) (rerr error)

Use opens a resource, calls Frob(input) on the result resource and then closes that resource (in all cases).

Types

type FrobError

type FrobError struct {
	// contains filtered or unexported fields
}

FrobError is a possible error from doing some frobbing, your implementation will require calling your Resource's Defrob(string) method. When this error occurs, the FrobError's defrobTag string will contain the string you must pass into Defrob.

func (FrobError) Error

func (e FrobError) Error() string

type Resource

type Resource interface {

	// Resource is using composition to inherit the requirements of the io.Closer
	// interface. What this means is that a Resource implementation will be
	// expected to have a .Close() method too.
	io.Closer

	// Frob does something with the input string.
	// Because this is an incredibly badly designed system if there is an error
	// it panics.
	//
	// The panicked error may be a FrobError in which case Defrob should be
	// called with the .defrobTag string property of the error.
	Frob(string)

	Defrob(string)
}

Resource represents abstract resource with close and frob methods.

type ResourceOpener

type ResourceOpener func() (Resource, error)

ResourceOpener is a function that creates a resource.

It may return a wrapped error of type TransientError. In this case the resource is temporarily unavailable and the caller should retry soon.

type TransientError

type TransientError struct {
	// contains filtered or unexported fields
}

TransientError is an error that may occur while opening a resource via ResourceOpener.

func (TransientError) Error

func (e TransientError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL