Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit cb7a17a

Browse files
committed
[CBR-26] Re-write and fix ToJSON/FromJSON instances for WalletError
Turns out that some of them were actually wrongly migrated (and therefore, fixing them to what they ought to be isn't a breaking change as mentioned before. This concerns the NodeIsStillSyncing and WalletIsNotReadyToProcessPayment branches). This rewrite also makes thing more readable, and now I just want to refactor that to use generics.....
1 parent df34520 commit cb7a17a

7 files changed

+159
-136
lines changed

wallet-new/src/Cardano/Wallet/API/Response.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ deriveGeneric ''JSONValidationError
243243

244244
instance ToJSON JSONValidationError where
245245
toEncoding (JSONValidationFailed weValidationError) = pairs $ mconcat
246-
[ "message" .= String "JSONValidationFailed"
247-
, "status" .= ErrorStatus
246+
[ "status" .= ErrorStatus
248247
, "diagnostic" .= object
249248
[ "validationError" .= weValidationError
250249
]
250+
, "message" .= String "JSONValidationFailed"
251251
]
252252

253253
instance FromJSON JSONValidationError where

wallet-new/src/Cardano/Wallet/API/V1/Migration/Types.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ deriveGeneric ''MigrationError
351351

352352
instance ToJSON MigrationError where
353353
toEncoding (MigrationFailed weDescription) = pairs $ mconcat
354-
[ "message" .= String "MigrationFailed"
355-
, "status" .= ErrorStatus
354+
[ "status" .= ErrorStatus
356355
, "diagnostic" .= object
357356
[ "description" .= weDescription
358357
]
358+
, "message" .= String "MigrationFailed"
359359
]
360360

361361
instance FromJSON MigrationError where

wallet-new/src/Cardano/Wallet/API/V1/Types.hs

+151-128
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ import Universum
103103

104104
import Control.Lens (At, Index, IxValue, at, ix, makePrisms, to, (?~))
105105
import Data.Aeson
106-
import Data.Aeson.Encoding (pairStr)
107106
import qualified Data.Aeson.Options as Serokell
108107
import Data.Aeson.TH as A
109108
import Data.Aeson.Types (Value (..), toJSONKeyText, typeMismatch)
@@ -112,7 +111,6 @@ import qualified Data.ByteArray as ByteArray
112111
import qualified Data.ByteString as BS
113112
import qualified Data.Char as C
114113
import Data.Default (Default (def))
115-
import qualified Data.HashMap.Strict as HMS
116114
import qualified Data.IxSet.Typed as IxSet
117115
import qualified Data.Map.Strict as Map
118116
import Data.Semigroup (Semigroup)
@@ -2377,131 +2375,156 @@ instance ToServantError WalletError where
23772375
instance ToHttpErrorStatus WalletError
23782376

23792377
instance ToJSON WalletError where
2380-
toEncoding (NotEnoughMoney weNeedMore) =
2381-
pairs $ ("status" .= ErrorStatus)
2382-
<> pairStr "diagnostic"
2383-
(pairs $ pairStr "needMore" (toEncoding weNeedMore))
2384-
<> "message" .= String "NotEnoughMoney"
2385-
toEncoding (OutputIsRedeem weAddress) =
2386-
pairs $ "status" .= ErrorStatus
2387-
<> pairStr "diagnostic"
2388-
(pairs $ pairStr "address" (toEncoding weAddress))
2389-
<> "message" .= String "OutputIsRedeem"
2390-
toEncoding (UnknownError weMsg) =
2391-
pairs $ "status" .= ErrorStatus
2392-
<> pairStr "diagnostic" (pairs $ pairStr "msg" (toEncoding weMsg))
2393-
<> "message" .= String "UnknownError"
2394-
toEncoding (InvalidAddressFormat weMsg) =
2395-
pairs $ "status" .= ErrorStatus
2396-
<> pairStr "diagnostic" (pairs $ pairStr "msg" (toEncoding weMsg))
2397-
<> "message" .= String "InvalidAddressFormat"
2398-
toEncoding (WalletNotFound) =
2399-
pairs $ "status" .= ErrorStatus
2400-
<> pairStr "diagnostic" (pairs $ mempty)
2401-
<> "message" .= String "WalletNotFound"
2402-
toEncoding (WalletAlreadyExists wid) =
2403-
pairs $ "status" .= ErrorStatus
2404-
<> pairStr "diagnostic" (pairs $ pairStr "walletId" (toEncoding wid))
2405-
<> "message" .= String "WalletAlreadyExists"
2406-
toEncoding (AddressNotFound) =
2407-
pairs $ "status" .= ErrorStatus
2408-
<> pairStr "diagnostic" (pairs $ mempty)
2409-
<> "message" .= String "AddressNotFound"
2410-
toEncoding (TxFailedToStabilize) =
2411-
pairs $ "status" .= ErrorStatus
2412-
<> pairStr "diagnostic" (pairs $ mempty)
2413-
<> "message" .= String "TxFailedToStabilize"
2414-
toEncoding (InvalidPublicKey weProblem) =
2415-
pairs $ "status" .= ErrorStatus
2416-
<> pairStr "diagnostic" (pairs $ pairStr "msg" (toEncoding weProblem))
2417-
<> "message" .= String "InvalidPublicKey"
2418-
toEncoding (UnsignedTxCreationError) =
2419-
pairs $ "status" .= ErrorStatus
2420-
<> pairStr "diagnostic" (pairs $ mempty)
2421-
<> "message" .= String "UnsignedTxCreationError"
2422-
toEncoding (TooBigTransaction) =
2423-
pairs $ "status" .= ErrorStatus
2424-
<> pairStr "diagnostic" (pairs $ mempty)
2425-
<> "message" .= String "TooBigTransaction"
2426-
toEncoding (SignedTxSubmitError weProblem) =
2427-
pairs $ "status" .= ErrorStatus
2428-
<> pairStr "diagnostic" (pairs $ pairStr "msg" (toEncoding weProblem))
2429-
<> "message" .= String "SignedTxSubmitError"
2430-
toEncoding (TxRedemptionDepleted) =
2431-
pairs $ "status" .= ErrorStatus
2432-
<> pairStr "diagnostic" (pairs $ mempty)
2433-
<> "message" .= String "TxRedemptionDepleted"
2434-
toEncoding (TxSafeSignerNotFound weAddress) =
2435-
pairs $ "status" .= ErrorStatus
2436-
<> pairStr "diagnostic"
2437-
(pairs $ pairStr "address" (toEncoding weAddress))
2438-
<> "message" .= String "TxSafeSignerNotFound"
2439-
toEncoding (MissingRequiredParams requiredParams) =
2440-
pairs $ "status" .= ErrorStatus
2441-
<> pairStr "diagnostic"
2442-
(pairs $ pairStr "params" (toEncoding requiredParams))
2443-
<> "message" .= String "MissingRequiredParams"
2444-
toEncoding (CannotCreateAddress weProblem) =
2445-
pairs $ "status" .= ErrorStatus
2446-
<> pairStr "diagnostic" (pairs $ pairStr "msg" (toEncoding weProblem))
2447-
<> "message" .= String "CannotCreateAddress"
2448-
toEncoding (WalletIsNotReadyToProcessPayments weStillRestoring) =
2449-
toEncoding $ toJSON weStillRestoring
2450-
toEncoding (NodeIsStillSyncing wenssStillSyncing) =
2451-
toEncoding $ toJSON wenssStillSyncing
2378+
toEncoding err = pairs $ mconcat $ ("status" .= ErrorStatus) : case err of
2379+
NotEnoughMoney v ->
2380+
[ "diagnostic" .= object [ "needMore" .= v ]
2381+
, "message" .= String "NotEnoughMoney"
2382+
]
2383+
2384+
OutputIsRedeem v ->
2385+
[ "diagnostic" .= object [ "address" .= v ]
2386+
, "message" .= String "OutputIsRedeem"
2387+
]
2388+
2389+
UnknownError v ->
2390+
[ "diagnostic" .= object [ "msg" .= v ]
2391+
, "message" .= String "UnknownError"
2392+
]
2393+
2394+
InvalidAddressFormat v ->
2395+
[ "diagnostic" .= object [ "msg" .= v ]
2396+
, "message" .= String "InvalidAddressFormat"
2397+
]
2398+
2399+
WalletNotFound ->
2400+
[ "diagnostic" .= object mempty
2401+
, "message" .= String "WalletNotFound"
2402+
]
2403+
2404+
WalletAlreadyExists v ->
2405+
[ "diagnostic" .= object [ "walletId" .= v ]
2406+
, "message" .= String "WalletAlreadyExists"
2407+
]
2408+
2409+
AddressNotFound ->
2410+
[ "diagnostic" .= object mempty
2411+
, "message" .= String "AddressNotFound"
2412+
]
2413+
2414+
InvalidPublicKey v ->
2415+
[ "diagnostic" .= object [ "msg" .= v ]
2416+
, "message" .= String "InvalidPublicKey"
2417+
]
2418+
2419+
TxFailedToStabilize ->
2420+
[ "diagnostic" .= object mempty
2421+
, "message" .= String "TxFailedToStabilize"
2422+
]
2423+
2424+
UnsignedTxCreationError ->
2425+
[ "diagnostic" .= object mempty
2426+
, "message" .= String "UnsignedTxCreationError"
2427+
]
2428+
2429+
TooBigTransaction ->
2430+
[ "diagnostic" .= object mempty
2431+
, "message" .= String "TooBigTransaction"
2432+
]
2433+
2434+
SignedTxSubmitError v ->
2435+
[ "diagnostic" .= object [ "msg" .= v ]
2436+
, "message" .= String "SignedTxSubmitError"
2437+
]
2438+
2439+
TxRedemptionDepleted ->
2440+
[ "diagnostic" .= object mempty
2441+
, "message" .= String "TxRedemptionDepleted"
2442+
]
2443+
2444+
TxSafeSignerNotFound v ->
2445+
[ "diagnostic" .= object [ "address" .= v ]
2446+
, "message" .= String "TxSafeSignerNotFound"
2447+
]
2448+
2449+
MissingRequiredParams v ->
2450+
[ "diagnostic" .= object [ "params" .= v ]
2451+
, "message" .= String "MissingRequiredParams"
2452+
]
2453+
2454+
CannotCreateAddress v ->
2455+
[ "diagnostic" .= object [ "msg" .= v ]
2456+
, "message" .= String "CannotCreateAddress"
2457+
]
2458+
2459+
WalletIsNotReadyToProcessPayments v ->
2460+
[ "diagnostic" .= object [ "stillRestoring" .= v ]
2461+
, "message" .= String "WalletIsNotReadyToProcessPayments"
2462+
]
2463+
2464+
NodeIsStillSyncing v ->
2465+
[ "diagnostic" .= object [ "stillSyncing" .= v ]
2466+
, "message" .= String "NodeIsStillSyncing"
2467+
]
2468+
24522469

24532470
instance FromJSON WalletError where
2454-
parseJSON (Object o)
2455-
| HMS.member "message" o =
2456-
case HMS.lookup "message" o of
2457-
Just "NotEnoughMoney" ->
2458-
NotEnoughMoney
2459-
<$> ((o .: "diagnostic") >>= (.: "needMore"))
2460-
Just "OutputIsRedeem" ->
2461-
OutputIsRedeem <$> ((o .: "diagnostic") >>= (.: "address"))
2462-
Just "UnknownError" ->
2463-
UnknownError <$> ((o .: "diagnostic") >>= (.: "msg"))
2464-
Just "InvalidAddressFormat" ->
2465-
InvalidAddressFormat
2466-
<$> ((o .: "diagnostic") >>= (.: "msg"))
2467-
Just "WalletNotFound" -> pure WalletNotFound
2468-
Just "WalletAlreadyExists" ->
2469-
WalletAlreadyExists <$> ((o .: "diagnostic") >>= (.: "walletId"))
2470-
Just "AddressNotFound" -> pure AddressNotFound
2471-
Just "TxFailedToStabilize" -> pure TxFailedToStabilize
2472-
Just "TxRedemptionDepleted" -> pure TxRedemptionDepleted
2473-
Just "TxSafeSignerNotFound" ->
2474-
TxSafeSignerNotFound
2475-
<$> ((o .: "diagnostic") >>= (.: "address"))
2476-
Just "InvalidPublicKey" ->
2477-
InvalidPublicKey <$> ((o .: "diagnostic") >>= (.: "msg"))
2478-
Just "UnsignedTxCreationError" -> pure UnsignedTxCreationError
2479-
Just "TooBigTransaction" -> pure TooBigTransaction
2480-
Just "SignedTxSubmitError" ->
2481-
SignedTxSubmitError <$> ((o .: "diagnostic") >>= (.: "msg"))
2482-
Just "CannotCreateAddress" ->
2483-
CannotCreateAddress <$> ((o .: "diagnostic") >>= (.: "msg"))
2484-
Just "MissingRequiredParams" ->
2485-
MissingRequiredParams
2486-
<$> ((o .: "diagnostic") >>= (.: "params"))
2487-
Just _ ->
2488-
fail "Incorrect JSON encoding for WalletError"
2489-
Nothing ->
2490-
fail "Incorrect JSON encoding for WalletError"
2491-
-- WalletIsNotReadyToProcessPayments
2492-
| HMS.member "estimatedCompletionTime" o = do
2493-
estCompTO <- (o .: "estimatedCompletionTime")
2494-
sThroughPO <- (o .: "throughput")
2495-
prctO <- (o .: "percentage")
2496-
estCompT <- parseJSON estCompTO
2497-
sThroughP <- parseJSON sThroughPO
2498-
prct <- parseJSON prctO
2499-
return . WalletIsNotReadyToProcessPayments
2500-
$ SyncProgress estCompT sThroughP prct
2501-
-- NodeIsStillSyncing
2502-
| HMS.member "quantity" o = do
2503-
quantityO <- o .: "quantity"
2504-
quantity <- parseJSON quantityO
2505-
return . NodeIsStillSyncing $ mkSyncPercentage quantity
2506-
| otherwise = fail "Incorrect JSON encoding for WalletError"
2507-
parseJSON invalid = typeMismatch "WalletError" invalid
2471+
parseJSON = withObject "WalletError" $ \o -> do
2472+
message <- o .: "message"
2473+
diag <- o .: "diagnostic"
2474+
case message :: Text of
2475+
"NotEnoughMoney" ->
2476+
NotEnoughMoney <$> (diag .: "needMore")
2477+
2478+
"OutputIsRedeem" ->
2479+
OutputIsRedeem <$> (diag .: "address")
2480+
2481+
"UnknownError" ->
2482+
UnknownError <$> (diag .: "msg")
2483+
2484+
"InvalidAddressFormat" ->
2485+
InvalidAddressFormat <$> (diag .: "msg")
2486+
2487+
"WalletNotFound" ->
2488+
pure WalletNotFound
2489+
2490+
"WalletAlreadyExists" ->
2491+
WalletAlreadyExists <$> (diag .: "walletId")
2492+
2493+
"AddressNotFound" ->
2494+
pure AddressNotFound
2495+
2496+
"TxFailedToStabilize" ->
2497+
pure TxFailedToStabilize
2498+
2499+
"TxRedemptionDepleted" ->
2500+
pure TxRedemptionDepleted
2501+
2502+
"TxSafeSignerNotFound" ->
2503+
TxSafeSignerNotFound <$> (diag .: "address")
2504+
2505+
"InvalidPublicKey" ->
2506+
InvalidPublicKey <$> (diag .: "msg")
2507+
2508+
"UnsignedTxCreationError" ->
2509+
pure UnsignedTxCreationError
2510+
2511+
"TooBigTransaction" ->
2512+
pure TooBigTransaction
2513+
2514+
"SignedTxSubmitError" ->
2515+
SignedTxSubmitError <$> (diag .: "msg")
2516+
2517+
"CannotCreateAddress" ->
2518+
CannotCreateAddress <$> (diag .: "msg")
2519+
2520+
"MissingRequiredParams" ->
2521+
MissingRequiredParams <$> (diag .: "params")
2522+
2523+
"WalletIsNotReadyToProcessPayments" ->
2524+
WalletIsNotReadyToProcessPayments <$> (diag .: "stillRestoring")
2525+
2526+
"NodeIsStillSyncing" ->
2527+
NodeIsStillSyncing <$> (diag .: "stillSyncing")
2528+
2529+
_ ->
2530+
fail "Incorrect JSON encoding for WalletError"
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"message":"JSONValidationFailed","status":"error","diagnostic":{"validationError":"test"}}
1+
{"status":"error","diagnostic":{"validationError":"test"},"message":"JSONValidationFailed"}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"message":"MigrationFailed","status":"error","diagnostic":{"description":"test"}}
1+
{"status":"error","diagnostic":{"description":"test"},"message":"MigrationFailed"}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"status":"error","diagnostic":{"quantity":14,"unit":"percent"},"message":"NodeIsStillSyncing"}
1+
{"status":"error","diagnostic":{"stillSyncing":{"quantity":14,"unit":"percent"}},"message":"NodeIsStillSyncing"}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"status":"error","diagnostic":{"throughput":{"quantity":400,"unit":"blocksPerSecond"},"percentage":{"quantity":14,"unit":"percent"},"estimatedCompletionTime":{"quantity":3000,"unit":"milliseconds"}},"message":"WalletIsNotReadyToProcessPayments"}
1+
{"status":"error","diagnostic":{"stillRestoring":{"throughput":{"quantity":400,"unit":"blocksPerSecond"},"percentage":{"quantity":14,"unit":"percent"},"estimatedCompletionTime":{"quantity":3000,"unit":"milliseconds"}}},"message":"WalletIsNotReadyToProcessPayments"}

0 commit comments

Comments
 (0)