mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-05 06:28:47 -04:00
fix: on the client, throw with the actual error message (#18)
* fix: on the client, throw with the actual error message * test: ensure that error message is correctly sent * fix: deliver all fields of the error to the client * fix: do not enumerate all the fields of error
This commit is contained in:
committed by
Zoltan Kochan
parent
95e22cf44e
commit
cfff22fbc6
@@ -62,18 +62,22 @@ export default function (
|
||||
|
||||
function fetch(limit: (fn: () => PromiseLike<object>) => Promise<object>, url: string, body: object): Promise<object | undefined> { // 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)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user