Skip to content

Commit

Permalink
Return some error codes when adding peers
Browse files Browse the repository at this point in the history
Partly addresses #925. Not finished.
  • Loading branch information
EvanHahn committed Oct 21, 2024
1 parent c511a60 commit ae24889
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/lib/error-with-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Create an `Error` with a `code` property.
*
* @example
* const err = new ErrorWithCode('INVALID_DATA', 'data was invalid')
* err.message
* // => 'data was invalid'
* err.code
* // => 'INVALID_DATA'
*/
export class ErrorWithCode extends Error {
/**
* @param {string} code
* @param {string} message
*/
constructor(code, message) {
super(message)
/** @readonly */ this.code = code
}
}
19 changes: 12 additions & 7 deletions src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { keyBy } from './lib/key-by.js'
import { abortSignalAny } from './lib/ponyfills.js'
import timingSafeEqual from './lib/timing-safe-equal.js'
import { isHostnameIpAddress } from './lib/is-hostname-ip-address.js'
import { ErrorWithCode } from './lib/error-with-code.js'
import { MEMBER_ROLE_ID, ROLES, isRoleIdForNewInvite } from './roles.js'
import { wsCoreReplicator } from './server/ws-core-replicator.js'
/**
Expand Down Expand Up @@ -273,10 +274,11 @@ export class MemberApi extends TypedEmitter {
baseUrl,
{ dangerouslyAllowInsecureConnections = false } = {}
) {
assert(
isValidServerBaseUrl(baseUrl, { dangerouslyAllowInsecureConnections }),
'Base URL is invalid'
)
if (
!isValidServerBaseUrl(baseUrl, { dangerouslyAllowInsecureConnections })
) {
throw new ErrorWithCode('INVALID_URL', 'Server base URL is invalid')
}

const { serverDeviceId } = await this.#addServerToProject(baseUrl)

Expand Down Expand Up @@ -319,13 +321,15 @@ export class MemberApi extends TypedEmitter {
err && typeof err === 'object' && 'message' in err
? err.message
: String(err)
throw new Error(
throw new ErrorWithCode(
'NETWORK_ERROR',
`Failed to add server peer due to network error: ${message}`
)
}

if (response.status !== 200 && response.status !== 201) {
throw new Error(
throw new ErrorWithCode(
'INVALID_SERVER_RESPONSE',
`Failed to add server peer due to HTTP status code ${response.status}`
)
}
Expand All @@ -344,7 +348,8 @@ export class MemberApi extends TypedEmitter {
)
return { serverDeviceId: responseBody.data.deviceId }
} catch (err) {
throw new Error(
throw new ErrorWithCode(
'INVALID_SERVER_RESPONSE',
"Failed to add server peer because we couldn't parse the response"
)
}
Expand Down
10 changes: 9 additions & 1 deletion test-e2e/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ test('invalid base URLs', async (t) => {
invalidUrls.map((url) =>
assert.rejects(
() => project.$member.addServerPeer(url),
/base url is invalid/i,
{
code: 'INVALID_URL',
message: /base url is invalid/i,
},
`${url} should be invalid`
)
)
Expand All @@ -79,6 +82,7 @@ test("fails if we can't connect to the server", async (t) => {
dangerouslyAllowInsecureConnections: true,
}),
{
code: 'NETWORK_ERROR',
message: /Failed to add server peer due to network error/,
}
)
Expand Down Expand Up @@ -108,6 +112,7 @@ test(
dangerouslyAllowInsecureConnections: true,
}),
{
code: 'INVALID_SERVER_RESPONSE',
message: `Failed to add server peer due to HTTP status code ${statusCode}`,
}
)
Expand Down Expand Up @@ -147,6 +152,7 @@ test(
dangerouslyAllowInsecureConnections: true,
}),
{
code: 'INVALID_SERVER_RESPONSE',
message:
"Failed to add server peer because we couldn't parse the response",
}
Expand Down Expand Up @@ -175,6 +181,8 @@ test("fails if first request succeeds but sync doesn't", async (t) => {
dangerouslyAllowInsecureConnections: true,
}),
{
// TODO
// code: 'INVALID_SERVER_RESPONSE',
message: /404/,
}
)
Expand Down
10 changes: 10 additions & 0 deletions test/lib/error-with-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { ErrorWithCode } from '../../src/lib/error-with-code.js'

test('ErrorWithCode', () => {
const err = new ErrorWithCode('MY_CODE', 'my message')
assert.equal(err.code, 'MY_CODE')
assert.equal(err.message, 'my message')
assert(err instanceof Error)
})

0 comments on commit ae24889

Please sign in to comment.