44 *--------------------------------------------------------------------------------------------*/
55
66import { Disposable , } from 'vs/base/common/lifecycle' ;
7- import { IUserData , IUserDataSyncStoreService , UserDataSyncStoreErrorCode , UserDataSyncStoreError , IUserDataSyncStore , getUserDataSyncStore , IUserDataAuthTokenService } from 'vs/platform/userDataSync/common/userDataSync' ;
7+ import { IUserData , IUserDataSyncStoreService , UserDataSyncErrorCode , UserDataSyncError , IUserDataSyncStore , getUserDataSyncStore , IUserDataAuthTokenService , SyncSource } from 'vs/platform/userDataSync/common/userDataSync' ;
88import { IRequestService , asText , isSuccess } from 'vs/platform/request/common/request' ;
99import { URI } from 'vs/base/common/uri' ;
1010import { joinPath } from 'vs/base/common/resources' ;
@@ -27,7 +27,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
2727 this . userDataSyncStore = getUserDataSyncStore ( configurationService ) ;
2828 }
2929
30- async read ( key : string , oldValue : IUserData | null ) : Promise < IUserData > {
30+ async read ( key : string , oldValue : IUserData | null , source ?: SyncSource ) : Promise < IUserData > {
3131 if ( ! this . userDataSyncStore ) {
3232 throw new Error ( 'No settings sync store url configured.' ) ;
3333 }
@@ -40,7 +40,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
4040 headers [ 'If-None-Match' ] = oldValue . ref ;
4141 }
4242
43- const context = await this . request ( { type : 'GET' , url, headers } , CancellationToken . None ) ;
43+ const context = await this . request ( { type : 'GET' , url, headers } , source , CancellationToken . None ) ;
4444
4545 if ( context . res . statusCode === 304 ) {
4646 // There is no new value. Hence return the old value.
@@ -59,7 +59,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
5959 return { ref, content } ;
6060 }
6161
62- async write ( key : string , data : string , ref : string | null ) : Promise < string > {
62+ async write ( key : string , data : string , ref : string | null , source ?: SyncSource ) : Promise < string > {
6363 if ( ! this . userDataSyncStore ) {
6464 throw new Error ( 'No settings sync store url configured.' ) ;
6565 }
@@ -70,12 +70,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
7070 headers [ 'If-Match' ] = ref ;
7171 }
7272
73- const context = await this . request ( { type : 'POST' , url, data, headers } , CancellationToken . None ) ;
74-
75- if ( context . res . statusCode === 412 ) {
76- // There is a new value. Throw Rejected Error
77- throw new UserDataSyncStoreError ( 'New data exists' , UserDataSyncStoreErrorCode . Rejected ) ;
78- }
73+ const context = await this . request ( { type : 'POST' , url, data, headers } , source , CancellationToken . None ) ;
7974
8075 if ( ! isSuccess ( context ) ) {
8176 throw new Error ( 'Server returned ' + context . res . statusCode ) ;
@@ -96,30 +91,45 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
9691 const url = joinPath ( URI . parse ( this . userDataSyncStore . url ) , 'resource' ) . toString ( ) ;
9792 const headers : IHeaders = { 'Content-Type' : 'text/plain' } ;
9893
99- const context = await this . request ( { type : 'DELETE' , url, headers } , CancellationToken . None ) ;
94+ const context = await this . request ( { type : 'DELETE' , url, headers } , undefined , CancellationToken . None ) ;
10095
10196 if ( ! isSuccess ( context ) ) {
10297 throw new Error ( 'Server returned ' + context . res . statusCode ) ;
10398 }
10499 }
105100
106- private async request ( options : IRequestOptions , token : CancellationToken ) : Promise < IRequestContext > {
101+ private async request ( options : IRequestOptions , source : SyncSource | undefined , token : CancellationToken ) : Promise < IRequestContext > {
107102 const authToken = await this . authTokenService . getToken ( ) ;
108103 if ( ! authToken ) {
109104 throw new Error ( 'No Auth Token Available.' ) ;
110105 }
111106 options . headers = options . headers || { } ;
112107 options . headers [ 'authorization' ] = `Bearer ${ authToken } ` ;
113108
114- const context = await this . requestService . request ( options , token ) ;
109+ let context ;
110+
111+ try {
112+ context = await this . requestService . request ( options , token ) ;
113+ } catch ( e ) {
114+ throw new UserDataSyncError ( `Connection refused for the request '${ options . url ?. toString ( ) } '.` , UserDataSyncErrorCode . ConnectionRefused , source ) ;
115+ }
115116
116117 if ( context . res . statusCode === 401 ) {
117118 // Throw Unauthorized Error
118- throw new UserDataSyncStoreError ( 'Unauthorized' , UserDataSyncStoreErrorCode . Unauthroized ) ;
119+ throw new UserDataSyncError ( `Request ' ${ options . url ?. toString ( ) } ' is not authorized.` , UserDataSyncErrorCode . Unauthroized , source ) ;
119120 }
120121
121- return context ;
122+ if ( context . res . statusCode === 412 ) {
123+ // There is a new value. Throw Rejected Error
124+ throw new UserDataSyncError ( `${ options . type } request '${ options . url ?. toString ( ) } ' failed with precondition. There is new data exists for this resource. Make the request again with latest data.` , UserDataSyncErrorCode . Rejected , source ) ;
125+ }
122126
127+ if ( context . res . statusCode === 413 ) {
128+ // Throw Too Large Payload Error
129+ throw new UserDataSyncError ( `${ options . type } request '${ options . url ?. toString ( ) } ' failed because data is too large.` , UserDataSyncErrorCode . TooLarge , source ) ;
130+ }
131+
132+ return context ;
123133 }
124134
125135}
0 commit comments