-
Notifications
You must be signed in to change notification settings - Fork 29
Return an error instead of throwing on invalid UTF-8 in deserialisation helpers #1249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
palas
wants to merge
3
commits into
master
Choose a base branch
from
nonthrowing-utf8-decode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changes/20260708_140000_cardano-api_pablo.lamela_nonthrowing_utf8_decode.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| description: | | ||
| Fixed `deserialiseInput`, `deserialiseInputAnyOf` and `deserialiseAnyVerificationKeyBech32` (and thereby `deserialiseAnyVerificationKey`) to return an error instead of throwing an impure exception when the input is not valid UTF-8. Added a `Bech32InvalidUtf8` constructor to `Bech32DecodeError`, which carries the rendered UTF-8 decoding error. | ||
| kind: | ||
| - bugfix | ||
| pr: 1249 | ||
| project: cardano-api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,6 @@ import Data.ByteString.Char8 qualified as BSC | |
| import Data.Char (toLower) | ||
| import Data.Data | ||
| import Data.List.NonEmpty (NonEmpty) | ||
| import Data.Text (Text) | ||
| import Data.Text.Encoding qualified as Text | ||
| import GHC.Exts (IsList (..)) | ||
| import Prettyprinter | ||
|
|
@@ -107,9 +106,6 @@ deserialiseInput | |
| deserialiseInput acceptedFormats inputBs = | ||
| go (toList acceptedFormats) | ||
| where | ||
| inputText :: Text | ||
| inputText = Text.decodeUtf8 inputBs | ||
|
|
||
| go :: [InputFormat a] -> Either InputDecodeError a | ||
| go [] = Left InputInvalidError | ||
| go (kf : kfs) = | ||
|
|
@@ -139,13 +135,19 @@ deserialiseInput acceptedFormats inputBs = | |
| Left _ -> DeserialiseInputErrorFormatMismatch | ||
|
|
||
| deserialiseBech32 :: SerialiseAsBech32 a => DeserialiseInputResult a | ||
| deserialiseBech32 = | ||
| case deserialiseFromBech32 inputText of | ||
| Right res -> DeserialiseInputSuccess res | ||
| -- The input was not valid Bech32. | ||
| Left (Bech32DecodingError _) -> DeserialiseInputErrorFormatMismatch | ||
| -- The input was valid Bech32, but some other error occurred. | ||
| Left err -> DeserialiseInputError $ InputBech32DecodeError err | ||
| deserialiseBech32 = either id fromBech32 $ decodeText inputBs | ||
| where | ||
| -- The input was not valid UTF-8, so it cannot be valid Bech32. | ||
| decodeText = | ||
| first (const DeserialiseInputErrorFormatMismatch) | ||
| . Text.decodeUtf8' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are still throwing away the exception error here. |
||
| fromBech32 text = | ||
| case deserialiseFromBech32 text of | ||
| Right res -> DeserialiseInputSuccess res | ||
| -- The input was not valid Bech32. | ||
| Left (Bech32DecodingError _) -> DeserialiseInputErrorFormatMismatch | ||
| -- The input was valid Bech32, but some other error occurred. | ||
| Left err -> DeserialiseInputError $ InputBech32DecodeError err | ||
|
|
||
| deserialiseHex :: SerialiseAsRawBytes a => DeserialiseInputResult a | ||
| deserialiseHex | ||
|
|
@@ -179,9 +181,6 @@ deserialiseInputAnyOf bech32Types textEnvTypes inputBs = | |
| DeserialiseInputError err -> Left err | ||
| DeserialiseInputErrorFormatMismatch -> Left InputInvalidError | ||
| where | ||
| inputText :: Text | ||
| inputText = Text.decodeUtf8 inputBs | ||
|
|
||
| orTry | ||
| :: DeserialiseInputResult b | ||
| -> DeserialiseInputResult b | ||
|
|
@@ -208,13 +207,19 @@ deserialiseInputAnyOf bech32Types textEnvTypes inputBs = | |
| Left _ -> DeserialiseInputErrorFormatMismatch | ||
|
|
||
| deserialiseBech32 :: DeserialiseInputResult b | ||
| deserialiseBech32 = | ||
| case deserialiseAnyOfFromBech32 bech32Types inputText of | ||
| Right res -> DeserialiseInputSuccess res | ||
| -- The input was not valid Bech32. | ||
| Left (Bech32DecodingError _) -> DeserialiseInputErrorFormatMismatch | ||
| -- The input was valid Bech32, but some other error occurred. | ||
| Left err -> DeserialiseInputError $ InputBech32DecodeError err | ||
| deserialiseBech32 = either id fromBech32 $ decodeText inputBs | ||
| where | ||
| -- The input was not valid UTF-8, so it cannot be valid Bech32. | ||
| decodeText = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are still throwing away the exception error here. |
||
| first (const DeserialiseInputErrorFormatMismatch) | ||
| . Text.decodeUtf8' | ||
| fromBech32 text = | ||
| case deserialiseAnyOfFromBech32 bech32Types text of | ||
| Right res -> DeserialiseInputSuccess res | ||
| -- The input was not valid Bech32. | ||
| Left (Bech32DecodingError _) -> DeserialiseInputErrorFormatMismatch | ||
| -- The input was valid Bech32, but some other error occurred. | ||
| Left err -> DeserialiseInputError $ InputBech32DecodeError err | ||
|
|
||
| -- | Read formatted file | ||
| readFormattedFile | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...-golden/files/errors/Cardano.Api.Serialise.Bech32.Bech32DecodeError/Bech32InvalidUtf8.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The Bech32-encoded string is not valid UTF-8: <text> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the field is
Textand not the exception itself? I'd suggest moving of rendering of the exception toPrettyinstance instead of keeping it in two places.