Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f/1776 add error handling #14

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,85 @@ describe('Output Plugin', () => {
// TODO test stream error
});

it('returns 400 when searchRequest returns 400', async () => {
[ plugin, app ] = buildPluginAndApp();

plugin.model = {
pullStream: jest.fn().mockRejectedValue({ status: 400, message: 'A validation error' })
}

await request(app)
.get('/dcat')
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(400)
.expect(res => {
expect(plugin.model.pullStream).toHaveBeenCalledTimes(1);
expect(res.body).toBeDefined();
expect(res.body.error).toEqual('A validation error');
});
});

it('returns 404 when provided domain does not exist and domain service returns appropriate message', async () => {
[ plugin, app ] = buildPluginAndApp();

mockFetchSite.mockRejectedValue({ message: 'DOMAIN DOES NOT EXIST :: 404' })
await request(app)
.get('/dcat')
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(404)
.expect(res => {
expect(res.body).toBeDefined();
expect(res.body.error).toEqual('DOMAIN DOES NOT EXIST :: 404');
});
});

it('returns 500 when provided domain does not exist and domain service returns wrong message', async () => {
[ plugin, app ] = buildPluginAndApp();

mockFetchSite.mockRejectedValue({ message: 'DOMAIN DOES NOT EXIST :: 403' })
await request(app)
.get('/dcat')
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(500)
.expect(res => {
expect(res.body).toBeDefined();
expect(res.body.error).toEqual('DOMAIN DOES NOT EXIST :: 403');
});
});

it('returns 404 when provided domain represents a private site', async () => {
[ plugin, app ] = buildPluginAndApp();

mockFetchSite.mockRejectedValue({ message: 'PRIVATE SITE', response: { error: { code: 403 } } })
await request(app)
.get('/dcat')
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(404)
.expect(res => {
expect(res.body).toBeDefined();
expect(res.body.error).toEqual('PRIVATE SITE');
});
});

it('returns 500 when ArcGIS returns wrong error for a site item', async () => {
[ plugin, app ] = buildPluginAndApp();

mockFetchSite.mockRejectedValue({ message: 'PRIVATE SITE' })
await request(app)
.get('/dcat')
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(500)
.expect(res => {
expect(res.body).toBeDefined();
expect(res.body.error).toEqual('PRIVATE SITE');
});
});

describe('configuration via query params', () => {
let mockGetDataStreamDcatUs11;

Expand Down
32 changes: 28 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as _ from 'lodash';

import { version } from '../package.json';
import { getDataStreamDcatUs11 } from './dcat-us';
import { fetchSite, getHubApiUrl, getPortalApiUrl, IHubRequestOptions } from '@esri/hub-common';
import { fetchSite, getHubApiUrl, getPortalApiUrl, IHubRequestOptions, RemoteServerError } from '@esri/hub-common';
import { IContentSearchRequest } from '@esri/hub-search';

const portalUrl = config.has('arcgisPortal')
Expand Down Expand Up @@ -34,7 +34,7 @@ export = class OutputDcatUs11 {

try {
const hostname = req.hostname;
const siteModel = await fetchSite(hostname, this.getRequestOptions(portalUrl));
const siteModel = await this.fetchSite(hostname, this.getRequestOptions(portalUrl));

// Use dcatConfig query param if provided, else default to site's config
let dcatConfig = typeof req.query.dcatConfig === 'string'
Expand All @@ -53,7 +53,7 @@ export = class OutputDcatUs11 {
const id = String(req.query.id || '');
req.res.locals.searchRequest = this.getDatasetSearchRequest(id, portalUrl, apiTerms) || this.getCatalogSearchRequest(_.get(siteModel, 'data.catalog'), portalUrl, apiTerms);

const datasetStream = await this.model.pullStream(req);
const datasetStream = await this.getDatasetStream(req);

datasetStream
.pipe(dcatStream)
Expand All @@ -62,7 +62,20 @@ export = class OutputDcatUs11 {
res.status(500).send(this.getErrorResponse(err));
});
} catch (err) {
res.status(500).send(this.getErrorResponse(err));
res.status(err.status || 500).send(this.getErrorResponse(err));
}
}

private async fetchSite(hostname: string, opts: IHubRequestOptions) {
try {
return await fetchSite(hostname, opts);
} catch (err) {

// Throw 404 if domain does not exist (first) or site is private (second)
if (err.message.includes(':: 404') || err.response?.error?.code === 403) {
throw new RemoteServerError(err.message, null, 404);
}
throw new RemoteServerError(err.message, null, 500);
}
}

Expand Down Expand Up @@ -119,6 +132,17 @@ export = class OutputDcatUs11 {
};
}

private async getDatasetStream(req: Request) {
try {
return await this.model.pullStream(req);
} catch (err) {
if (err.status === 400) {
throw new RemoteServerError(err.message, null, 400);
}
throw new RemoteServerError(err.message, null, 500);
}
}

private getErrorResponse(err: any) {
return {
error: _.get(
Expand Down