Skip to content

Commit

Permalink
Added error parsing to OAuth signup form (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov committed Feb 14, 2020
1 parent 5e996b3 commit d1787b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserPropertiesContract } from "../../../../../contracts/user";
import { Utils } from "../../../../../utils";
import { RouteHelper } from "../../../../../routing/routeHelper";
import { IAuthenticator } from "./../../../../../authentication/IAuthenticator";
import { MapiError } from "../../../../../services/mapiError";


@RuntimeComponent({
Expand Down Expand Up @@ -65,7 +66,7 @@ export class SignupSocial {
}

const jwtToken = Utils.parseJwt(idToken);

this.firstName(jwtToken.given_name);
this.lastName(jwtToken.family_name);
this.email(jwtToken.email);
Expand Down Expand Up @@ -96,6 +97,10 @@ export class SignupSocial {
body: JSON.stringify(user)
});

if (!(response.statusCode >= 200 && response.statusCode <= 299)) {
throw MapiError.fromResponse(response);
}

const sasTokenHeader = response.headers.find(x => x.name.toLowerCase() === "ocp-apim-sas-token");

if (!sasTokenHeader) { // User not registered with APIM.
Expand Down
11 changes: 11 additions & 0 deletions src/services/mapiError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HttpResponse } from "@paperbits/common/http";

export interface SmapiErrorDetails {
target: string;
message: string;
Expand All @@ -13,6 +15,15 @@ export class MapiError extends Error {
Object.setPrototypeOf(this, MapiError.prototype);
}

public static fromResponse(response: HttpResponse<any>): MapiError {
const responseObject = response.toObject();
const code = responseObject?.error?.code || "Error";
const message = responseObject?.error?.message || "Server error";
const details = responseObject?.error?.details || [];

return new MapiError(code, message, details);
}

public toString(): string {
return `${this.code}: ${this.message}`;
}
Expand Down

0 comments on commit d1787b2

Please sign in to comment.