fix(onboarding): fix error handling for invalid account data from server

Summary:
Change `addAccountFromJSON` in `AccountStore` to throw an exception when it encounters
invalid data, instead of calling `atom.emitError()`. Just calling `emitError` doesn't
throw an exception, so the code proceeds as if an account has been added. Instead, call
`emitError` from AccountSettingsPage, and display an error message to the user.

Test Plan: manual

Reviewers: bengotow

Reviewed By: bengotow

Subscribers: evan

Differential Revision: https://phab.nylas.com/D2116
This commit is contained in:
Drew Regitsky 2015-10-05 17:31:29 -07:00
parent b38ca54b45
commit 62fb708797
2 changed files with 14 additions and 5 deletions

View file

@ -45,7 +45,7 @@ class AccountSettingsPage extends React.Component
{data} = account_data {data} = account_data
# accountJson = @_decrypt(data, @state.provider.encryptionKey, @state.provider.encryptionIv) # accountJson = @_decrypt(data, @state.provider.encryptionKey, @state.provider.encryptionIv)
account = JSON.parse(data) account = JSON.parse(data)
OnboardingActions.accountJSONReceived(account) @_onAccountReceived(account)
else if tries < 20 and id is pollAttemptId else if tries < 20 and id is pollAttemptId
setTimeout(_retry, delay) setTimeout(_retry, delay)
delay *= 1.2 # exponential backoff delay *= 1.2 # exponential backoff
@ -251,11 +251,21 @@ class AccountSettingsPage extends React.Component
method: "POST" method: "POST"
timeout: 30000 timeout: 30000
body: json body: json
success: (json) => success: @_onAccountReceived
OnboardingActions.accountJSONReceived(json)
error: @_onNetworkError error: @_onNetworkError
.catch(@_onNetworkError) .catch(@_onNetworkError)
_onAccountReceived: (json) =>
try
OnboardingActions.accountJSONReceived(json)
catch e
atom.emitError(e)
@setState
tryingToAuthenticate: false
errorMessage: "Sorry, something went wrong on the Nylas server. Please try again. If you're still having issues, contact us at support@nylas.com."
@_resize()
_onNetworkError: (err) => _onNetworkError: (err) =>
errorMessage = err.message errorMessage = err.message
if errorMessage == "Invite code required" if errorMessage == "Invite code required"

View file

@ -79,8 +79,7 @@ class AccountStore
if not json.email_address or not json.provider if not json.email_address or not json.provider
console.error("Returned account data is invalid", json) console.error("Returned account data is invalid", json)
console.log JSON.stringify(json) console.log JSON.stringify(json)
atom.emitError(new Error("Returned account data is invalid")) throw new Error("Returned account data is invalid")
return
return if @_tokens[json.id] return if @_tokens[json.id]
@_tokens[json.id] = json.auth_token @_tokens[json.id] = json.auth_token
@_accounts.push((new Account).fromJSON(json)) @_accounts.push((new Account).fromJSON(json))