throw meaningful errors

This commit is contained in:
Karol Wypchlo 2022-01-14 11:32:24 +01:00
parent 185e9b8982
commit fbe108a573
No known key found for this signature in database
GPG Key ID: B515DE9EEBE241E1
1 changed files with 6 additions and 0 deletions

View File

@ -69,9 +69,15 @@ function getAuthCookie() {
// extract set-cookie from successful authentication request
const cookies = response.headers["set-cookie"];
// throw meaningful error when set-cookie header is missing
if (!cookies) throw new Error(`Auth successful (code ${response.statusCode}) but 'set-cookie' header is missing`);
// find the skynet-jwt cookie
const jwtcookie = cookies.find((cookie) => cookie.startsWith("skynet-jwt"));
// throw meaningful error when skynet-jwt cookie is missing
if (!jwtcookie) throw new Error(`Header 'set-cookie' found but 'skynet-jwt' cookie is missing`);
// extract just the cookie value (no set-cookie props) from set-cookie
return jwtcookie.match(/skynet-jwt=[^;]+;/)[0];
} catch (error) {