diff --git a/src/connectStoreController.ts b/src/connectStoreController.ts index ccb70f54d9..fdcf39ba5b 100644 --- a/src/connectStoreController.ts +++ b/src/connectStoreController.ts @@ -62,18 +62,22 @@ export default function ( function fetch(limit: (fn: () => PromiseLike) => Promise, url: string, body: object): Promise { // tslint:disable-line return limit(async () => { - const response = await got(url, { - body: JSON.stringify(body), - headers: {'Content-Type': 'application/json'}, - method: 'POST', - retries: () => { - return 100 - }, - }) - if (!response.body) { - return undefined + try { + const response = await got(url, { + body: JSON.stringify(body), + headers: {'Content-Type': 'application/json'}, + method: 'POST', + retries: () => { + return 100 + }, + }) + if (!response.body) { + return undefined + } + return JSON.parse(response.body) + } catch (e) { + throw JSON.parse(e.response.body) } - return JSON.parse(response.body) }) } diff --git a/src/createServer.ts b/src/createServer.ts index f848c21911..fbee4dafa4 100644 --- a/src/createServer.ts +++ b/src/createServer.ts @@ -134,7 +134,9 @@ export default function ( } } catch (e) { res.statusCode = 503 - res.end(JSON.stringify(e.message)) + const jsonErr = JSON.parse(JSON.stringify(e)) + jsonErr.message = e.message + res.end(JSON.stringify(jsonErr)) } }) diff --git a/test/index.ts b/test/index.ts index 548e663fd7..d3b8826414 100644 --- a/test/index.ts +++ b/test/index.ts @@ -79,6 +79,44 @@ test('server', async t => { t.end() }) +test('server errors should arrive to the client', async t => { + const port = 5813 + const hostname = '127.0.0.1' + const remotePrefix = `http://${hostname}:${port}` + const storeCtrlForServer = await createStoreController() + const server = createServer(storeCtrlForServer, { + port, + hostname, + }) + const storeCtrl = await connectStoreController({remotePrefix, concurrency: 100}) + let caught = false + try { + await storeCtrl.requestPackage( + {alias: 'not-an-existing-package', pref: '1.0.0'}, + { + downloadPriority: 0, + loggedPkg: {rawSpec: 'sfdf'}, + prefix: process.cwd(), + registry, + verifyStoreIntegrity: false, + preferredVersions: {}, + } + ) + } catch (e) { + caught = true + t.equal(e.message, '404 Not Found: not-an-existing-package', 'error message delivered correctly') + t.equal(e.code, 'E404', 'error code delivered correctly') + t.ok(e.uri, 'error uri field delivered') + t.ok(e.response, 'error response field delivered') + t.ok(e.package, 'error package field delivered') + } + t.ok(caught, 'exception raised correctly') + + await server.close() + await storeCtrl.close() + t.end() +}) + test('server upload', async t => { const port = 5813 const hostname = '127.0.0.1'