qfpl / applied-fp-course

Applied Functional Programming Course - Move from exercises to a working app!
http://qfpl.io/projects/professional-fp-courses/
Other
625 stars 180 forks source link

DocTests in src/Level06/Conf/File.hs are out of date #80

Closed ScottSedgwick closed 5 years ago

ScottSedgwick commented 5 years ago

In src/Level06/Conf/File.hs: Change

import           Level06.AppM               (AppM)

to

import           Level06.AppM               (AppM(runAppM))

and change

-- >>> readConfFile "badFileName.no"
-- Left (undefined "badFileName.no: openBinaryFile: does not exist (No such file or directory)")
-- >>> readConfFile "files/test.json"
-- Right "{\n  \"foo\": 33\n}\n"

to

-- >>> runAppM $ readConfFile "badFileName.no"
-- *** Exception: badFileName.no: openBinaryFile: does not exist (No such file or directory)
-- >>> runAppM $ readConfFile "files/test.json"
-- Right "{\"foo\":33}\n"
mankyKitty commented 5 years ago

Adding runAppM is correct. However you've updated the test to suit what your code was doing, not what the test is trying to check for, which is not correct.

The expectation is that you implement the readConfFile function so when you runAppM the value that is returned is Right when you successfully read the file, or Left with reason that reading the file failed. Using the try function or something else from Control.Exception we're able to execute functions and not let exceptions escape.

This will require that you be explicit about what type of exception you're going to want to catch. Since we're reading a file, the choice of IOException is the most appropriate. Since things like ArithException and AsyncException don't make much sense in this context.

The undefined can then be updated to be the constructor from ConfigError that you create to wrap the exception. Using show on the exception is sufficient for this exercise.