Skip to content

Commit

Permalink
Update registration and sso actions to use AppError
Browse files Browse the repository at this point in the history
  • Loading branch information
mnzaki committed May 10, 2019
1 parent c1bd51f commit 5bd6828
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
8 changes: 7 additions & 1 deletion src/actions/registration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { JolocomLib } from 'jolocom-lib'
import { SoftwareKeyProvider } from 'jolocom-lib/js/vaultedKeyProvider/softwareProvider'
const bip39 = require('bip39')
import { generateSecureRandomBytes } from 'src/lib/util'
import { AppError, ErrorCode } from 'src/lib/errors'

export const setLoadingMsg = (loadingMsg: string) => ({
type: 'SET_LOADING_MSG',
Expand Down Expand Up @@ -98,6 +99,11 @@ export const createIdentity = (encodedEntropy: string) => async (
}),
)
} catch (error) {
return dispatch(genericActions.showErrorScreen(error, routeList.Landing))
return dispatch(
genericActions.showErrorScreen(
new AppError(ErrorCode.RegistrationFailed, error),
routeList.Landing,
),
)
}
}
14 changes: 10 additions & 4 deletions src/actions/sso/authenticationRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { routeList } from 'src/routeList'
import { cancelSSO, clearInteractionRequest } from '.'
import { Linking } from 'react-native'
import { JolocomLib } from 'jolocom-lib'
import { AppError, ErrorCode } from 'src/lib/errors'

export const setAuthenticationRequest = (
request: StateAuthenticationRequestSummary,
Expand Down Expand Up @@ -39,9 +40,11 @@ export const consumeAuthenticationRequest = (
routeName: routeList.AuthenticationConsent,
}),
)
dispatch(ssoActions.setDeepLinkLoading(false))
} catch (err) {
dispatch(showErrorScreen(new Error('Authentication request failed.')))
dispatch(
showErrorScreen(new AppError(ErrorCode.AuthenticationRequestFailed, err)),
)
} finally {
dispatch(ssoActions.setDeepLinkLoading(false))
}
}
Expand Down Expand Up @@ -83,8 +86,11 @@ export const sendAuthenticationResponse = () => async (
}).then(() => dispatch(cancelSSO()))
}
} catch (err) {
console.log(err)
dispatch(clearInteractionRequest())
dispatch(showErrorScreen(new Error('Sending payment response failed.')))
dispatch(
showErrorScreen(
new AppError(ErrorCode.AuthenticationResponseFailed, err),
),
)
}
}
58 changes: 21 additions & 37 deletions src/actions/sso/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { KeyTypes } from 'jolocom-lib/js/vaultedKeyProvider/types'
import { consumePaymentRequest } from './paymentRequest'
import { Authentication } from 'jolocom-lib/js/interactionTokens/authentication'
import { consumeAuthenticationRequest } from './authenticationRequest'
import { AppError, ErrorCode } from 'src/lib/errors'

export const setCredentialRequest = (
request: StateCredentialRequestSummary,
Expand Down Expand Up @@ -90,10 +91,9 @@ export const parseJWT = (encodedJwt: string) => async (
return new Error('Unknown interaction type when parsing JWT')
}
} catch (err) {
console.log('error: ', err)
dispatch(accountActions.toggleLoading(false))
dispatch(setDeepLinkLoading(false))
dispatch(showErrorScreen(err))
dispatch(showErrorScreen(new AppError(ErrorCode.ParseJWTFailed, err)))
}
}

Expand Down Expand Up @@ -130,7 +130,9 @@ export const consumeCredentialOfferRequest = (
} catch (err) {
dispatch(accountActions.toggleLoading(false))
dispatch(setDeepLinkLoading(false))
dispatch(showErrorScreen(new Error('JWT Token parse failed')))
dispatch(
showErrorScreen(new AppError(ErrorCode.CredentialOfferFailed, err)),
)
}
}

Expand All @@ -145,16 +147,6 @@ export const receiveExternalCredential = (

try {
await identityWallet.validateJWT(credReceive, undefined, registry)
} catch (error) {
console.log(error)
dispatch(
showErrorScreen(
new Error('Validation of external credential token failed'),
),
)
}

try {
const providedCredentials = credReceive.interactionToken.signedCredentials

const results = await Promise.all(
Expand All @@ -167,25 +159,22 @@ export const receiveExternalCredential = (
}),
)

if (results.every(el => el === true)) {
dispatch(setReceivingCredential(providedCredentials))
dispatch(accountActions.toggleLoading(false))
dispatch(
navigationActions.navigatorReset({
routeName: routeList.CredentialDialog,
}),
)
} else {
dispatch(accountActions.toggleLoading(false))
dispatch(showErrorScreen(new Error('Signature validation failed')))
if (!results.every(el => el === true)) {
throw new Error('Signature validation failed')
}

dispatch(setReceivingCredential(providedCredentials))
dispatch(
navigationActions.navigatorReset({
routeName: routeList.CredentialDialog,
}),
)
} catch (error) {
dispatch(accountActions.toggleLoading(false))
dispatch(
showErrorScreen(
new Error('Signature validation on external credential failed'),
),
showErrorScreen(new AppError(ErrorCode.CredentialsReceiveFailed, error)),
)
} finally {
dispatch(accountActions.toggleLoading(false))
}
}

Expand Down Expand Up @@ -266,15 +255,14 @@ export const consumeCredentialRequest = (
}

dispatch(setCredentialRequest(summary))
dispatch(accountActions.toggleLoading(false))
dispatch(navigationActions.navigatorReset({ routeName: routeList.Consent }))
dispatch(setDeepLinkLoading(false))
} catch (error) {
console.log(error)
dispatch(accountActions.toggleLoading(false))
dispatch(
showErrorScreen(new Error('Consumption of credential request failed')),
showErrorScreen(new AppError(ErrorCode.CredentialRequestFailed, error)),
)
} finally {
dispatch(accountActions.toggleLoading(false))
}
}

Expand Down Expand Up @@ -338,14 +326,10 @@ export const sendCredentialResponse = (
}).then(() => dispatch(cancelSSO()))
}
} catch (error) {
// TODO: better error message
dispatch(clearInteractionRequest())
console.log(error)
dispatch(accountActions.toggleLoading(false))
dispatch(
showErrorScreen(
new Error('The credential response could not be created'),
),
showErrorScreen(new AppError(ErrorCode.CredentialResponseFailed, error)),
)
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/actions/sso/paymentRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JolocomLib } from 'jolocom-lib'
import { Linking } from 'react-native'
import { cancelSSO, clearInteractionRequest } from 'src/actions/sso'
import { JolocomRegistry } from 'jolocom-lib/js/registries/jolocomRegistry'
import { AppError, ErrorCode } from 'src/lib/errors'

export const setPaymentRequest = (request: StatePaymentRequestSummary) => ({
type: 'SET_PAYMENT_REQUEST',
Expand Down Expand Up @@ -47,10 +48,10 @@ export const consumePaymentRequest = (
dispatch(
navigationActions.navigatorReset({ routeName: routeList.PaymentConsent }),
)
dispatch(ssoActions.setDeepLinkLoading(false))
} catch (err) {
dispatch(showErrorScreen(new AppError(ErrorCode.PaymentRequestFailed, err)))
} finally {
dispatch(ssoActions.setDeepLinkLoading(false))
dispatch(showErrorScreen(new Error('Consuming payment request failed.')))
}
}

Expand Down Expand Up @@ -92,8 +93,9 @@ export const sendPaymentResponse = () => async (
}).then(() => dispatch(cancelSSO()))
}
} catch (err) {
console.log(err)
dispatch(clearInteractionRequest())
dispatch(showErrorScreen(new Error('Sending payment response failed.')))
dispatch(
showErrorScreen(new AppError(ErrorCode.PaymentResponseFailed, err)),
)
}
}

0 comments on commit 5bd6828

Please sign in to comment.