Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
fix : error handling in components using vuex store services
Browse files Browse the repository at this point in the history
  • Loading branch information
mrellipse committed Jul 13, 2017
1 parent 3c761fb commit 70bef23
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/ui/app/components/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class Login extends Vue {
.then((value) => this.$store.dispatch(StoreTypes.updateUser, value))
.then(() => this.$store.dispatch(StoreTypes.updateStatusBar, null))
.then(() => this.$router.push(returnUrl))
.catch(() => { });
}

loginExternal(providerId: string): void {
Expand All @@ -119,7 +120,7 @@ export class Login extends Vue {
if (!client) {

let msg: IPayloadMessage = {
text: 'Unknown authentication provider \'' + providerId +'\'',
text: 'Unknown authentication provider \'' + providerId + '\'',
messageTypeId: PayloadMessageTypes.warning
}

Expand Down
4 changes: 2 additions & 2 deletions src/ui/app/components/verify/verify.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h3 class="mb-2">{{$t('verify.title')}}</h3>

<div class="form-group" v-show="!verifyCodeIssued">
<p>{{$t('verify.sendCode')}}</p>
<button class="btn btn-primary" @click.prevent="issueVerificationCode()"><i class="fa fa-lg fa-lock" aria-hidden="true"></button>
<button class="btn btn-primary" @click.prevent="issueVerificationCode()"><i class="fa fa-lg fa-lock" aria-hidden="true"></i></button>
</div>

<div class="form-group" v-show="verifyCodeIssued">
Expand All @@ -13,7 +13,7 @@ <h3 class="mb-2">{{$t('verify.title')}}</h3>
<span class="alert-danger" v-if="!$v.verifyCode.required">{{$t('validation.required')}}</span>
</span>
</p>
<button class="btn btn-primary" @click.prevent="redeemVerificationCode()"><i class="fa fa-lg fa-unlock" aria-hidden="true"></button>
<button class="btn btn-primary" @click.prevent="redeemVerificationCode()"><i class="fa fa-lg fa-unlock" aria-hidden="true"></i></button>
</div>

</main>
7 changes: 5 additions & 2 deletions src/ui/app/components/verify/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class Verify extends Vue {
issueVerificationCode() {

let onSuccess = (code: string) => {

console.info(`${code}`);
this.verifyCodeIssued = true;

Expand All @@ -42,7 +43,8 @@ export class Verify extends Vue {
};

this.auth.verify()
.then(onSuccess);
.then(onSuccess)
.catch(() => { });
}

redeemVerificationCode() {
Expand Down Expand Up @@ -71,7 +73,8 @@ export class Verify extends Vue {

this.auth.redeemVerificationCode(this.verifyCode)
.then(onSuccess)
.then(onStoreDispatch);
.then(onStoreDispatch)
.catch(() => { });
}

verifyCodeIssued: boolean = false;
Expand Down
7 changes: 3 additions & 4 deletions src/ui/app/model/payload/payload-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,20 @@ export class PayloadMapper {
text: null
}
};

return value;
}

public fromObject<T>(o: any): IPayload<T> {

if (isAxiosResponse(o))
return this.fromAxiosResponse<T>(o);

if (isAxiosError(o))
return this.fromAxiosError<T>(o);

if (o instanceof Error)
return this.fromError<T>(o);

if (isAxiosResponse(o))
return this.fromAxiosResponse<T>(o);

return null;
}
}
Expand Down
70 changes: 23 additions & 47 deletions src/ui/app/services/authentication/authentication-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,18 @@ export class AuthenticationService extends StoreService implements IClaimsHelper

login(credentials: ICredential) {

let onSuccess = (payload: IPayload<IAccessToken>) => {
let onSuccess = (token: IAccessToken) => {

if (token) {

if (payload.message.messageTypeId === PayloadMessageTypes.success) {

let token = payload.data.access_token;
TokenHelper.setAccessToken(token.access_token);

TokenHelper.setAccessToken(token);

return TokenHelper.parseUserToken(token);
} else {
throw new Error(payload.message.text);
return TokenHelper.parseUserToken(token.access_token);
}
}

return this.exec(Axios.post(LOGIN_URL, credentials))
.then(value => this.processPayload(value))
.then(onSuccess);
}

Expand All @@ -94,18 +91,14 @@ export class AuthenticationService extends StoreService implements IClaimsHelper

redeemAccessToken(provider: ILoginProvider) {

let onSuccess = (payload: IPayload<IAccessToken>) => {

if (payload.message.messageTypeId === PayloadMessageTypes.success) {
let onSuccess = (token: IAccessToken) => {

let token = payload.data.access_token;
if (token) {

TokenHelper.setAccessToken(token);
TokenHelper.setAccessToken(token.access_token);
TokenHelper.removeProvider();

return TokenHelper.parseUserToken(token);
} else {
throw new Error(payload.message.text);
return TokenHelper.parseUserToken(token.access_token);
}
};

Expand All @@ -116,23 +109,19 @@ export class AuthenticationService extends StoreService implements IClaimsHelper
}

return this.exec(Axios.post(REDEEM_ACCESS_TOKEN, data))
.then(value => this.processPayload(value))
.then(onSuccess);
}

redeemVerificationCode(code: string) {

let onSuccess = (payload: IPayload<IAccessToken>) => {

if (payload.message.messageTypeId === PayloadMessageTypes.success) {

let token = payload.data.access_token;
let onSuccess = (token: IAccessToken) => {

TokenHelper.setAccessToken(token);
if (token) {

return TokenHelper.parseUserToken(token);
TokenHelper.setAccessToken(token.access_token);

} else {
throw new Error(payload.message.text);
return TokenHelper.parseUserToken(token.access_token);
}
};

Expand All @@ -141,42 +130,29 @@ export class AuthenticationService extends StoreService implements IClaimsHelper
};

return this.exec(Axios.put(REDEEM_VERIFICATION_CODE, null, config))
.then(value => this.processPayload(value))
.then(onSuccess);
}

signup(signup: ISignupOptions) {

let onSuccess = (payload: IPayload<IAccessToken>) => {
let onSuccess = (token: IAccessToken) => {

if (payload.message.messageTypeId === PayloadMessageTypes.success) {
if (token) {

let token = payload.data.access_token;
TokenHelper.setAccessToken(token.access_token);

TokenHelper.setAccessToken(token);

return TokenHelper.parseUserToken(token);
} else {
throw new Error(payload.message.text);
return TokenHelper.parseUserToken(token.access_token);
}
}

return this.exec(Axios.post(SIGNUP_URL, signup))
.then(value => this.processPayload(value))
.then(onSuccess);
}

verify() {
let onSuccess = (res: AxiosResponse) => {

let payload: IPayload<string> = res.data;

if (payload.message.messageTypeId === PayloadMessageTypes.success) {
return payload.data;
} else {
throw new Error(payload.message.text);
}
}

return Axios.get(VERIFICATION_CODE_URL)
.then(onSuccess);
return this.exec(Axios.get(VERIFICATION_CODE_URL))
.then(value => this.processPayload<string>(<any>value))
}
}
16 changes: 10 additions & 6 deletions src/ui/app/store/store-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface IStoreService {

interface IProcessPayloadOptions<T> {
timeout?: number,
uri?: string
uri?: string,
messageTypeIds?: string[]
}

export abstract class StoreService implements IStoreService {
Expand All @@ -22,10 +23,10 @@ export abstract class StoreService implements IStoreService {

}

handleFulfilled<T>(value: T) {
handleFulfilled<T>(reason: T) {

return this.store.dispatch(StoreTypes.loadingState, false)
.then(() => new PayloadMapper().fromObject(value));
.then(() => new PayloadMapper().fromObject(reason));
}

handleRejection<T>(reason: any) {
Expand All @@ -39,8 +40,11 @@ export abstract class StoreService implements IStoreService {

let message = payload.message;
options = options || {};
let messageTypeIds = options.messageTypeIds || [PayloadMessageTypes.error, PayloadMessageTypes.failure];

let messageTypeId = messageTypeIds.find(o => o === message.messageTypeId);

if (message.messageTypeId === PayloadMessageTypes.error) {
if (messageTypeId) {

options.timeout = options.timeout || 1500;

Expand All @@ -53,8 +57,8 @@ export abstract class StoreService implements IStoreService {
};

return this.store.dispatch(StoreTypes.updateStatusBar, statusBarData)
.then(() => Promise.resolve(null));
.then(() => Promise.reject(null));

} else {
return Promise.resolve(payload.data);
}
Expand Down

0 comments on commit 70bef23

Please sign in to comment.