Giter VIP home page Giter VIP logo

Comments (6)

sol avatar sol commented on September 28, 2024

I have been looking at Stack's exceptions, as part of the Haskell Foundation's Haskell Error Index initiative. That has taken me to the Pantry library, and then from Pantry to Hpack.

For GHC I see value in having identifiable and stable error messages. In the past I've been parsing GHC error messages myself and both the fact that GHC pretty prints them (leading to variations in line breaks and indentation) and that the format is not stable across GHC versions is a pain.

For Hpack I don't have the insight to judge if effort spent here will provide benefits that amortize that effort.

However, from the perspective of a user of the Hpack API I still think it's reasonable to want access to parse errors etc.

At the moment, Hpack uses String as its exception type and ends with System.Exit.die :: String -> IO a. This prints an error message to stderr and then throws an uninformative exception (ExitFailure 1). I propose to introduce an HpackException type for Hpack exceptions, which would produce the error message when the exception is thrown. That type would allow users of the Hpack library to catch the exceptions and give a different output to the one Hpack chooses to deliver, if they wished.

Exceptions are for unexpected situations. That a user has a syntax error in package.yaml is not something out of the ordinary. For that reason I don't think that exceptions are necessarily the best option here. How about adding a new primitive to the API that returns IO (Either String Result) or IO (Either SomeErrorType Result)

from hpack.

mpilgrem avatar mpilgrem commented on September 28, 2024

@sol, thanks. Currently, Hpack has (extracts):

type Errors = ExceptT String

readPackageConfig :: DecodeOptions -> IO (Either String DecodeResult)

and, where the action is yielded:

hpackResultWithVersion :: Version -> Options -> IO Result
hpackResultWithVersion v (Options options force generateHashStrategy toStdout) = do
  DecodeResult pkg (lines -> cabalVersion) cabalFileName warnings <- readPackageConfig options >>= either die return

That is, Hpack uses String as its exeception type and throws an exception (in hpackResultWithVersion) if there is a syntax error in package.yaml or some other 'exception'.

My pull request (#531) replaces that with:

type Errors = ExceptT HpackException

readPackageConfig :: DecodeOptions -> IO (Either HpackException DecodeResult)

and

hpackResultWithVersion :: Version -> Options -> IO Result
hpackResultWithVersion v (Options options force generateHashStrategy toStdout) = do
  DecodeResult pkg (lines -> cabalVersion) cabalFileName warnings <- readPackageConfig options >>= either throwIO return

that is, rather than hpackResultWithVersion: (1) sending an error message to stderr and throwing uninformative ExitFailure 1 (die); (2) it thows an informative exception (throwIO) - if that is not caught and handled, the exception will send exactly the same error message to stderr as in hpack-0.35.

from hpack.

mpilgrem avatar mpilgrem commented on September 28, 2024

As an example of what end users experience, pantry-0.8.0 handles exceptions thrown by Hpack. Currently, a user of Stack would receive output like this:

D:\Users\mike\Code\Haskell\foo\package.yaml:61:0: could not find expected ':' while scanning a simple key
Error: [S-305]
Failed to generate a Cabal file using the Hpack library on file:
D:\Users\mike\Code\Haskell\foo\package.yaml

The exception encountered was:

ExitFailure 1

The first line is what Hpack outputs into stderr. The rest is Pantry's handling of the Hpack exception ExitFailure 1.

After pull request #531, the user of Stack would receive this output:

Error: [S-305]
Failed to generate a Cabal file using the Hpack library on file:
D:\Users\mike\Code\Haskell\foo\package.yaml

The exception encountered was:

D:\Users\mike\Code\Haskell\foo\package.yaml:61:0: could not find expected ':' while scanning a simple key

Hpack's message is conveyed in the informative exception.

from hpack.

sol avatar sol commented on September 28, 2024

@mpilgrem I would love to discuss this at length, but the amount of time I'm able to spend on this is limited. For that reason we have to handle this efficiently.

I looked at your PR and I estimate that we will need five rounds of review to get this in shape. To avoid any further confusion I will leave a comment on your PR.

from hpack.

sol avatar sol commented on September 28, 2024

(2) it thows an informative exception (throwIO)

That's what I'm suggesting instead:

How about adding a new primitive to the API that returns IO (Either String Result) or IO (Either SomeErrorType Result)

from hpack.

sol avatar sol commented on September 28, 2024

Addressed by #535.

from hpack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.