Source: PersistQuotaExceededError.js

  1. import {getStorage, getStorageType} from "./storageManager";
  2. const setPrototypeOf = Object.setPrototypeOf || ((obj, proto) => {
  3. // eslint-disable-next-line no-proto
  4. obj.__proto__ = proto;
  5. return obj;
  6. });
  7. /**
  8. * Special type of known error that {@link Persist} throws.
  9. * @ko Persist 내부에서 알려진 오류 발생시 throw되는 에러
  10. * @property {string} key Error key <ko>에러가 되는 키</ko>
  11. * @property {string} message Error message <ko>에러 메시지</ko>
  12. * @property {"SessionStorage" | "LocalStorage" | "History" | "None"} storageType The storage type in which the error occurred <ko>에러가 발생한 스토리지 타입</ko>
  13. * @property {number} size The size of the value in which the error occurred <ko>에러가 발생한 값의 사이즈</ko>
  14. * @property {Object} values Values of high size in storage. (maxLengh: 3) <ko>스토리지의 높은 사이즈의 값들. (최대 3개)</ko>
  15. * @example
  16. * ```ts
  17. * import Persist, { PersistQuotaExceededError } from "@egjs/persist";
  18. * try {
  19. * const persist = new Persist("key");
  20. * } catch (e) {
  21. * if (e instanceof PersistQuotaExceededError) {
  22. * console.error("size", e.size);
  23. * }
  24. * }
  25. * ```
  26. */
  27. class PersistQuotaExceededError extends Error {
  28. /**
  29. * @param key Error message<ko>에러 메시지</ko>
  30. * @param value Error value<ko>에러 값</ko>
  31. */
  32. constructor(key, value) {
  33. const size = value.length;
  34. const storageType = getStorageType();
  35. const storage = getStorage();
  36. let valuesText = "";
  37. let values = [];
  38. if (storage) {
  39. const length = storage.length;
  40. for (let i = 0; i < length; ++i) {
  41. const itemKey = storage.key(i);
  42. const item = storage.getItem(itemKey) || "";
  43. values.push({key: itemKey, size: item.length});
  44. }
  45. values = values.sort((a, b) => b.size - a.size).slice(0, 3);
  46. if (values.length) {
  47. valuesText = ` The highest values of ${storageType} are ${values.map(item => JSON.stringify({[item.key]: item.size})).join(", ")}.`;
  48. }
  49. }
  50. super(`Setting the value (size: ${size}) of '${key}' exceeded the ${storageType}'s quota.${valuesText}`);
  51. setPrototypeOf(this, PersistQuotaExceededError.prototype);
  52. this.name = "PersistQuotaExceededError";
  53. this.storageType = storageType;
  54. this.key = key;
  55. this.size = size;
  56. this.values = values;
  57. }
  58. }
  59. export default PersistQuotaExceededError;
comments powered by Disqus