let storage: { [key: string]: string } = {}
return {
getItem: (key: string) => (key in storage ? storage[key] : null),
- setItem: (key: string, value: string) => (storage[key] = value || ''),
+ setItem: (key: string, value: string) => {
+ if (value.length > 1000000) throw new Error('localStorage overflow')
+ storage[key] = value || ''
+ },
removeItem: (key: string) => delete storage[key],
clear: () => (storage = {}),
}
HttpTestingController,
} from '@angular/common/http/testing'
import { environment } from 'src/environments/environment'
-import { Subscription } from 'rxjs'
+import { Subscription, throwError } from 'rxjs'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { ConfirmDialogComponent } from '../components/common/confirm-dialog/confirm-dialog.component'
import { OPEN_DOCUMENT_SERVICE } from '../data/storage-keys'
+import { wind } from 'ngx-bootstrap-icons'
const documents = [
{
req.error(new ErrorEvent('timeout'))
expect(openDocumentsService.getOpenDocuments()).toHaveLength(0)
})
+
+ it('should log error on sessionStorage save', () => {
+ const doc = { ...documents[0] }
+ doc.content = 'a'.repeat(1000000)
+ const consoleSpy = jest.spyOn(console, 'error').mockImplementation()
+ openDocumentsService.openDocument(doc)
+ expect(consoleSpy).toHaveBeenCalled()
+ })
})
}
save() {
- sessionStorage.setItem(
- OPEN_DOCUMENT_SERVICE.DOCUMENTS,
- JSON.stringify(this.openDocuments)
- )
+ try {
+ sessionStorage.setItem(
+ OPEN_DOCUMENT_SERVICE.DOCUMENTS,
+ JSON.stringify(this.openDocuments)
+ )
+ } catch (e) {
+ console.error('Error saving open documents to session storage', e)
+ }
}
}