@@ -277,6 +277,10 @@ export class MemberApi extends TypedEmitter {
277
277
* peer. For example, the project must have a name.
278
278
* - `NETWORK_ERROR`: there was an issue connecting to the server. Is the
279
279
* device online? Is the server online?
280
+ * - `SERVER_HAS_TOO_MANY_PROJECTS`: the server limits the number of projects
281
+ * it can have, and it's at the limit.
282
+ * - `PROJECT_NOT_IN_SERVER_ALLOWLIST`: the server only allows specific
283
+ * projects to be added and ours wasn't one of them.
280
284
* - `INVALID_SERVER_RESPONSE`: we connected to the server but it returned
281
285
* an unexpected response. Is the server running a compatible version of
282
286
* CoMapeo Cloud?
@@ -351,32 +355,7 @@ export class MemberApi extends TypedEmitter {
351
355
)
352
356
}
353
357
354
- if ( response . status !== 200 && response . status !== 201 ) {
355
- throw new ErrorWithCode (
356
- 'INVALID_SERVER_RESPONSE' ,
357
- `Failed to add server peer due to HTTP status code ${ response . status } `
358
- )
359
- }
360
-
361
- try {
362
- const responseBody = await response . json ( )
363
- assert (
364
- responseBody &&
365
- typeof responseBody === 'object' &&
366
- 'data' in responseBody &&
367
- responseBody . data &&
368
- typeof responseBody . data === 'object' &&
369
- 'deviceId' in responseBody . data &&
370
- typeof responseBody . data . deviceId === 'string' ,
371
- 'Response body is valid'
372
- )
373
- return { serverDeviceId : responseBody . data . deviceId }
374
- } catch ( err ) {
375
- throw new ErrorWithCode (
376
- 'INVALID_SERVER_RESPONSE' ,
377
- "Failed to add server peer because we couldn't parse the response"
378
- )
379
- }
358
+ return await parseAddServerResponse ( response )
380
359
}
381
360
382
361
/**
@@ -575,3 +554,66 @@ function isValidServerBaseUrl(
575
554
function encodeBufferForServer ( buffer ) {
576
555
return buffer ? b4a . toString ( buffer , 'hex' ) : undefined
577
556
}
557
+
558
+ /**
559
+ * @param {Response } response
560
+ * @returns {Promise<{ serverDeviceId: string }> }
561
+ */
562
+ async function parseAddServerResponse ( response ) {
563
+ if ( response . status === 200 ) {
564
+ try {
565
+ const responseBody = await response . json ( )
566
+ assert (
567
+ responseBody &&
568
+ typeof responseBody === 'object' &&
569
+ 'data' in responseBody &&
570
+ responseBody . data &&
571
+ typeof responseBody . data === 'object' &&
572
+ 'deviceId' in responseBody . data &&
573
+ typeof responseBody . data . deviceId === 'string' ,
574
+ 'Response body is valid'
575
+ )
576
+ return { serverDeviceId : responseBody . data . deviceId }
577
+ } catch ( err ) {
578
+ throw new ErrorWithCode (
579
+ 'INVALID_SERVER_RESPONSE' ,
580
+ "Failed to add server peer because we couldn't parse the response"
581
+ )
582
+ }
583
+ }
584
+
585
+ let responseBody
586
+ try {
587
+ responseBody = await response . json ( )
588
+ } catch ( _ ) {
589
+ responseBody = null
590
+ }
591
+ if (
592
+ responseBody &&
593
+ typeof responseBody === 'object' &&
594
+ 'error' in responseBody &&
595
+ responseBody . error &&
596
+ typeof responseBody . error === 'object' &&
597
+ 'code' in responseBody . error
598
+ ) {
599
+ switch ( responseBody . error . code ) {
600
+ case 'PROJECT_NOT_IN_ALLOWLIST' :
601
+ throw new ErrorWithCode (
602
+ 'PROJECT_NOT_IN_SERVER_ALLOWLIST' ,
603
+ "The server only allows specific projects to be added, and this isn't one of them"
604
+ )
605
+ case 'TOO_MANY_PROJECTS' :
606
+ throw new ErrorWithCode (
607
+ 'SERVER_HAS_TOO_MANY_PROJECTS' ,
608
+ "The server limits the number of projects it can have and it's at the limit"
609
+ )
610
+ default :
611
+ break
612
+ }
613
+ }
614
+
615
+ throw new ErrorWithCode (
616
+ 'INVALID_SERVER_RESPONSE' ,
617
+ `Failed to add server peer due to HTTP status code ${ response . status } `
618
+ )
619
+ }
0 commit comments