-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add PersistQuotaExceededError (#51)
* feat: add PersistQuotaExceededError * test: test PersistQuotaExceededError * fix: catch error in window env * feat: show storage's values * skip: apply review
- Loading branch information
Showing
11 changed files
with
295 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {getStorage, getStorageType} from "./storageManager"; | ||
|
||
const setPrototypeOf = Object.setPrototypeOf || ((obj, proto) => { | ||
// eslint-disable-next-line no-proto | ||
obj.__proto__ = proto; | ||
return obj; | ||
}); | ||
|
||
|
||
/** | ||
* Special type of known error that {@link Persist} throws. | ||
* @ko Persist 내부에서 알려진 오류 발생시 throw되는 에러 | ||
* @property {string} key Error key <ko>에러가 되는 키</ko> | ||
* @property {string} message Error message <ko>에러 메시지</ko> | ||
* @property {"SessionStorage" | "LocalStorage" | "History" | "None"} storageType The storage type in which the error occurred <ko>에러가 발생한 스토리지 타입</ko> | ||
* @property {number} size The size of the value in which the error occurred <ko>에러가 발생한 값의 사이즈</ko> | ||
* @property {Object} values Values of high size in storage. (maxLengh: 3) <ko>스토리지의 높은 사이즈의 값들. (최대 3개)</ko> | ||
* @example | ||
* ```ts | ||
* import Persist, { PersistQuotaExceededError } from "@egjs/persist"; | ||
* try { | ||
* const persist = new Persist("key"); | ||
* } catch (e) { | ||
* if (e instanceof PersistQuotaExceededError) { | ||
* console.error("size", e.size); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
class PersistQuotaExceededError extends Error { | ||
/** | ||
* @param key Error message<ko>에러 메시지</ko> | ||
* @param value Error value<ko>에러 값</ko> | ||
*/ | ||
constructor(key, value) { | ||
const size = value.length; | ||
const storageType = getStorageType(); | ||
const storage = getStorage(); | ||
let valuesText = ""; | ||
let values = []; | ||
|
||
if (storage) { | ||
const length = storage.length; | ||
|
||
for (let i = 0; i < length; ++i) { | ||
const itemKey = storage.key(i); | ||
const item = storage.getItem(itemKey) || ""; | ||
|
||
values.push({key: itemKey, size: item.length}); | ||
} | ||
values = values.sort((a, b) => b.size - a.size).slice(0, 3); | ||
|
||
if (values.length) { | ||
valuesText = ` The highest values of ${storageType} are ${values.map(item => JSON.stringify({[item.key]: item.size})).join(", ")}.`; | ||
} | ||
} | ||
|
||
super(`Setting the value (size: ${size}) of '${key}' exceeded the ${storageType}'s quota.${valuesText}`); | ||
|
||
setPrototypeOf(this, PersistQuotaExceededError.prototype); | ||
this.name = "PersistQuotaExceededError"; | ||
this.storageType = storageType; | ||
this.key = key; | ||
this.size = size; | ||
this.values = values; | ||
} | ||
} | ||
|
||
export default PersistQuotaExceededError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Persist, {updateDepth} from "./Persist"; | ||
import PersistQuotaExceededError from "./PersistQuotaExceededError"; | ||
|
||
|
||
export { | ||
updateDepth, | ||
PersistQuotaExceededError, | ||
}; | ||
|
||
export default Persist; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import Persist, {updateDepth} from "./Persist"; | ||
import Persist, * as modules from "./index"; | ||
|
||
// eslint-disable-next-line import/no-named-as-default-member | ||
Persist.updateDepth = updateDepth; | ||
for (const name in modules) { | ||
Persist[name] = modules[name]; | ||
} | ||
|
||
export default Persist; |
Oops, something went wrong.