Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

fromBytes throws exceptions instead of returning the error inside the Maybe ValidationFailure #312

Closed
jship opened this issue Jun 10, 2017 · 4 comments
Labels
B Bug T - array affect type Arrays T - string Affect type Strings

Comments

@jship
Copy link
Member

jship commented Jun 10, 2017

The following program throws a ByteOutOfBound exception instead of providing the error in the Maybe ValidationError in the triple returned by fromBytes.

#!/usr/bin/env stack
-- stack script --resolver lts-8.17 --package foundation
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad
import Foundation
import Foundation.String
import Foundation.String.ASCII
import Foreign.C.Types

printString :: Encoding -> UArray Word8 -> IO ()
printString enc bytes = do
  -- fromBytes :: Encoding -> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
  let (string, minvalid, rest) = fromBytes enc bytes
  putStrLn string
  case minvalid of
    Nothing -> putStrLn "Valid!"
    (Just vf) -> putStrLn "Invalid!"
  when (null rest) $ do
    putStrLn "rest is empty"
  unless (null rest) $ do
    putStrLn "rest is not empty"

main = printString ASCII7 (fromList [0xff])
strings: ByteOutOfBound 255
@NicolasDP NicolasDP added B Bug T - array affect type Arrays T - string Affect type Strings labels Jun 10, 2017
@NicolasDP
Copy link
Member

@jship, is it a bug you are happy to look at ?

@jship
Copy link
Member Author

jship commented Jun 10, 2017

Yes, will take a look at it.

@jship
Copy link
Member Author

jship commented Jun 11, 2017

@NicolasDP: Can think of a few options to this and would like to discuss with you.

  1. Do something similar to what UTF8 does with its validate function. UTF8 doesn't use the fromEncodeBytes machinery.
  2. Update Builder itself to wrap an error transformer.
  3. Update builderBuild to no longer discard the monadic result and return possible error there.

Interested in pairing on this today with myself and @Tiltman?

@NicolasDP
Copy link
Member

I think you are looking at the right solution. We could try some stuff with the Builder

We can extend it with MonadThrow and MonadCatch. This will allow us to do some error handling/reporting in convertFromTo and the Encoding .encodingWrite specialised function.

vincenthz added a commit that referenced this issue Jun 15, 2017
Fix #312: Refactor Builder to support possibility of failure.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
B Bug T - array affect type Arrays T - string Affect type Strings
Projects
None yet
Development

No branches or pull requests

2 participants