@@ -43,6 +43,14 @@ const enum AESConstants {
43
43
IV_LENGTH = 12 ,
44
44
}
45
45
46
+ class NetworkError extends Error {
47
+ constructor ( inner : Error ) {
48
+ super ( inner . message ) ;
49
+ this . name = inner . name ;
50
+ this . stack = inner . stack ;
51
+ }
52
+ }
53
+
46
54
class ServerKeyedAESCrypto implements ISecretStorageCrypto {
47
55
private _serverKey : Uint8Array | undefined ;
48
56
@@ -138,30 +146,33 @@ class ServerKeyedAESCrypto implements ISecretStorageCrypto {
138
146
}
139
147
140
148
let attempt = 0 ;
141
- let lastError : unknown | undefined ;
149
+ let lastError : Error | undefined ;
142
150
143
151
while ( attempt <= 3 ) {
144
152
try {
145
153
const res = await fetch ( this . authEndpoint , { credentials : 'include' , method : 'POST' } ) ;
146
154
if ( ! res . ok ) {
147
155
throw new Error ( res . statusText ) ;
148
156
}
149
- const serverKey = new Uint8Array ( await await res . arrayBuffer ( ) ) ;
157
+ const serverKey = new Uint8Array ( await res . arrayBuffer ( ) ) ;
150
158
if ( serverKey . byteLength !== AESConstants . KEY_LENGTH / 8 ) {
151
159
throw Error ( `The key retrieved by the server is not ${ AESConstants . KEY_LENGTH } bit long.` ) ;
152
160
}
153
161
this . _serverKey = serverKey ;
154
162
return this . _serverKey ;
155
163
} catch ( e ) {
156
- lastError = e ;
164
+ lastError = e instanceof Error ? e : new Error ( String ( e ) ) ;
157
165
attempt ++ ;
158
166
159
167
// exponential backoff
160
168
await new Promise ( resolve => setTimeout ( resolve , attempt * attempt * 100 ) ) ;
161
169
}
162
170
}
163
171
164
- throw lastError ;
172
+ if ( lastError ) {
173
+ throw new NetworkError ( lastError ) ;
174
+ }
175
+ throw new Error ( 'Unknown error' ) ;
165
176
}
166
177
}
167
178
@@ -187,7 +198,9 @@ export class LocalStorageSecretStorageProvider implements ISecretStorageProvider
187
198
} catch ( err ) {
188
199
// TODO: send telemetry
189
200
console . error ( 'Failed to decrypt secrets from localStorage' , err ) ;
190
- localStorage . removeItem ( this . _storageKey ) ;
201
+ if ( ! ( err instanceof NetworkError ) ) {
202
+ localStorage . removeItem ( this . _storageKey ) ;
203
+ }
191
204
}
192
205
}
193
206
0 commit comments