Skip to content

Commit

Permalink
FetchService throws dedicated exception when server unreachable
Browse files Browse the repository at this point in the history
+ Clears up confusion on app init / first auth check call if back-end UI server is down
+ Fixes #315
  • Loading branch information
cnrudd authored and amcclain committed May 24, 2018
1 parent ef9cea3 commit c0b7ec4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 1 addition & 2 deletions core/XH.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ class XhModel {
async getAuthUserFromServerAsync() {
return await this.fetchService
.fetchJson({url: 'auth/authUser'})
.then(r => r.authUser)
.catch(() => null);
.then(r => r.authUser);
}

async initServicesAsync() {
Expand Down
14 changes: 14 additions & 0 deletions exception/Exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ export class Exception {
return this.createInternal(defaults, {});
}

static serverUnavailable(url, requestOptions, e) {
const match = url.match(/^[a-z]+:\/\/[^/]+/i),
origin = match ? match[0] : window.location.origin,
message = `Unable to contact the server at ${origin}`;

return this.createInternal({
name: 'Server Unavailable',
message: message,
originalMessage: e.message,
url: url,
requestOptions: requestOptions
});
}

//-----------------------
// Implementation
//-----------------------
Expand Down
8 changes: 7 additions & 1 deletion svc/FetchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ export class FetchService {
}
}

const ret = await fetch(url, opts);
let ret;
try {
ret = await fetch(url, opts);
} catch (e) {
throw Exception.serverUnavailable(url, opts, e);
}
if (!ret.ok) throw Exception.requestError(opts, ret);
return ret;

}

/**
Expand Down

0 comments on commit c0b7ec4

Please sign in to comment.