Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
improve error handling which was broken by migration from superagent …
Browse files Browse the repository at this point in the history
…to fetch
  • Loading branch information
RusAlex committed Oct 28, 2016
1 parent 79f7201 commit ea40d12
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
53 changes: 47 additions & 6 deletions src/api/client.js
Expand Up @@ -43,20 +43,41 @@ export default class ApiClient
return format_url(urlObj);
}

async handleResponseError(response) {
if (!response.ok) {
let json, errorMessage;

errorMessage = response.statusText;

try {
json = await response.json();
errorMessage = json.error || errorMessage;
} catch (e) {
throw new Error(errorMessage);
}

throw new Error(errorMessage);
}
}

async get(relativeUrl, query = {}) {
let defaultHeaders = {};

if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
defaultHeaders = { Cookie: this.serverReq.headers['cookie'] };
}

return fetch(
const response = await fetch(
this.apiUrlForFetch(relativeUrl, query),
{
credentials: 'same-origin',
headers: defaultHeaders
}
);

await this.handleResponseError(response);

return response;
}

async head(relativeUrl, query = {}) {
Expand All @@ -83,14 +104,17 @@ export default class ApiClient
defaultHeaders = { Cookie: this.serverReq.headers['cookie'] };
}

return fetch(
const response = await fetch(
this.apiUrlForFetch(relativeUrl),
{
credentials: 'same-origin',
method: 'DELETE',
headers: defaultHeaders
}
);
await this.handleResponseError(response);

return response;
}

async post(relativeUrl, data = null) {
Expand All @@ -110,7 +134,7 @@ export default class ApiClient
body = stringify(data);
}

return fetch(
const response = await fetch(
this.apiUrl(relativeUrl),
{
credentials: 'same-origin',
Expand All @@ -119,6 +143,10 @@ export default class ApiClient
body
}
);

await this.handleResponseError(response);

return response;
}

/*
Expand All @@ -139,7 +167,7 @@ export default class ApiClient
body = data;
}

return fetch(
const response = await fetch(
this.apiUrl(relativeUrl),
{
credentials: 'same-origin',
Expand All @@ -148,6 +176,9 @@ export default class ApiClient
body
}
);
await this.handleResponseError(response);

return response;
}

async postJSON(relativeUrl, data = null) {
Expand All @@ -167,7 +198,7 @@ export default class ApiClient
body = JSON.stringify(data);
}

return fetch(
const response = await fetch(
this.apiUrl(relativeUrl),
{
credentials: 'same-origin',
Expand All @@ -176,6 +207,9 @@ export default class ApiClient
body
}
);
await this.handleResponseError(response);

return response;
}

async subscriptions(offset = 0) {
Expand Down Expand Up @@ -234,7 +268,14 @@ export default class ApiClient

async relatedPosts(postId) {
const response = await this.get(`/api/v1/post/${postId}/related-posts`);
return await response.json();

const json = await response.json();

if (!response.ok) {
throw new Error(json.error);
}

return json;
}

async userTags() {
Expand Down
32 changes: 19 additions & 13 deletions src/api/controller.js
Expand Up @@ -1703,19 +1703,24 @@ export default class ApiController {

getUser = async (ctx) => {
const User = this.bookshelf.model('User');
const u = await User
.where({ username: ctx.params.username })
.fetch({
require: true,
withRelated: [
'following', 'followers', 'liked_posts',
'liked_hashtags', 'liked_schools', 'liked_geotags',
'favourited_posts', 'followed_hashtags',
'followed_schools', 'followed_geotags'
]
});

ctx.body = u.toJSON();
try {
const u = await User
.where({ username: ctx.params.username })
.fetch({
require: true,
withRelated: [
'following', 'followers', 'liked_posts',
'liked_hashtags', 'liked_schools', 'liked_geotags',
'favourited_posts', 'followed_hashtags',
'followed_schools', 'followed_geotags'
]
});
ctx.body = u.toJSON();
} catch (e) {
ctx.status = 404;
return;
}
};

followUser = async (ctx) => {
Expand Down Expand Up @@ -2701,7 +2706,8 @@ export default class ApiController {
ctx.body = posts;
} catch (e) {
ctx.status = 500;
ctx.body = { error: e.message };
ctx.body = { error: 'Internal Server Error' };
ctx.app.logger.error(e);
}
};

Expand Down

0 comments on commit ea40d12

Please sign in to comment.