Ticket #510 (closed enhancement: invalid)

Opened 4 years ago

Last modified 4 years ago

Don't call exitWith on parse error, instead return error or throw exception

Reported by: guest Owned by:
Priority: normal Milestone:
Component: Cabal library Version: 1.6.0.1
Severity: minor Keywords:
Cc: Difficulty: unknown
GHC Version: Platform:

Description

Please consider modifying readPackageDescription so that it doesn't end up calling dieWithLocation. It seems the decision of what to do when a parse error occurs is better made in the application rather than the library.

Change History

Changed 4 years ago by guest

  • summary changed from Don't call exitWithFailure on parse error, instead return error or throw exception to Don't call exitWith on parse error, instead return error or throw exception

Changed 4 years ago by duncan

  • status changed from new to closed
  • resolution set to invalid

If you want to handle the parse errors you can use:

parsePackageDescription :: String -> ParseResult GenericPackageDescription

The ParseResult gives you everything about the parsing outcome. It includes a structured description of any parse error or warnings. There are functions provided to format these errors and warnings into strings if you don't want to handle them individually.

By contrast, readPackageDescription is an "all in one" convenience function that works in IO, reads the file and throws exceptions in IO if reading the file or parsing it fails.

readPackageDescription :: Verbosity -> FilePath -> IO GenericPackageDescription

Admitedly, it does use the wrong exception if parsing fails, it uses the ExitWith exception where it should use UserError. It is still catchable however. But you clearly don't want to be using exceptions here, you want to use the pure function parsePackageDescription that gives you the chance to handle parse errors yourself.

I'm closing this ticket but opening a new one about the issue with die throwing the wrong kind of exception.

Changed 4 years ago by duncan

Oops, should have linked to the new ticket about die: #512.

Note: See TracTickets for help on using tickets.