@@ -103,7 +103,6 @@ import Universum
103
103
104
104
import Control.Lens (At , Index , IxValue , at , ix , makePrisms , to , (?~) )
105
105
import Data.Aeson
106
- import Data.Aeson.Encoding (pairStr )
107
106
import qualified Data.Aeson.Options as Serokell
108
107
import Data.Aeson.TH as A
109
108
import Data.Aeson.Types (Value (.. ), toJSONKeyText , typeMismatch )
@@ -112,7 +111,6 @@ import qualified Data.ByteArray as ByteArray
112
111
import qualified Data.ByteString as BS
113
112
import qualified Data.Char as C
114
113
import Data.Default (Default (def ))
115
- import qualified Data.HashMap.Strict as HMS
116
114
import qualified Data.IxSet.Typed as IxSet
117
115
import qualified Data.Map.Strict as Map
118
116
import Data.Semigroup (Semigroup )
@@ -2377,131 +2375,156 @@ instance ToServantError WalletError where
2377
2375
instance ToHttpErrorStatus WalletError
2378
2376
2379
2377
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
+
2452
2469
2453
2470
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"
0 commit comments