@atlaskit/media-client 29.1.0 → 30.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/cjs/client/file-fetcher/error.js +12 -10
  3. package/dist/cjs/client/file-fetcher/index.js +28 -46
  4. package/dist/cjs/client/media-store/error.js +4 -5
  5. package/dist/cjs/index.js +12 -0
  6. package/dist/cjs/models/errors/index.js +68 -5
  7. package/dist/cjs/models/errors/types.js +5 -1
  8. package/dist/cjs/uploader/error.js +11 -9
  9. package/dist/cjs/uploader/index.js +2 -1
  10. package/dist/cjs/utils/mobileUpload/error.js +12 -10
  11. package/dist/cjs/utils/mobileUpload/helpers.js +4 -2
  12. package/dist/cjs/utils/polling/errors.js +6 -7
  13. package/dist/cjs/utils/polling/index.js +6 -2
  14. package/dist/cjs/utils/request/errors.js +4 -6
  15. package/dist/cjs/utils/request/helpers.js +8 -3
  16. package/dist/es2019/client/file-fetcher/error.js +10 -7
  17. package/dist/es2019/client/file-fetcher/index.js +22 -45
  18. package/dist/es2019/client/media-store/error.js +4 -3
  19. package/dist/es2019/index.js +1 -1
  20. package/dist/es2019/models/errors/index.js +62 -5
  21. package/dist/es2019/models/errors/types.js +1 -0
  22. package/dist/es2019/uploader/error.js +10 -7
  23. package/dist/es2019/uploader/index.js +2 -1
  24. package/dist/es2019/utils/mobileUpload/error.js +10 -7
  25. package/dist/es2019/utils/mobileUpload/helpers.js +4 -2
  26. package/dist/es2019/utils/polling/errors.js +8 -5
  27. package/dist/es2019/utils/polling/index.js +6 -2
  28. package/dist/es2019/utils/request/errors.js +4 -4
  29. package/dist/es2019/utils/request/helpers.js +8 -3
  30. package/dist/esm/client/file-fetcher/error.js +12 -10
  31. package/dist/esm/client/file-fetcher/index.js +29 -47
  32. package/dist/esm/client/media-store/error.js +4 -5
  33. package/dist/esm/index.js +1 -1
  34. package/dist/esm/models/errors/index.js +67 -4
  35. package/dist/esm/models/errors/types.js +1 -0
  36. package/dist/esm/uploader/error.js +11 -9
  37. package/dist/esm/uploader/index.js +2 -1
  38. package/dist/esm/utils/mobileUpload/error.js +12 -10
  39. package/dist/esm/utils/mobileUpload/helpers.js +4 -2
  40. package/dist/esm/utils/polling/errors.js +6 -7
  41. package/dist/esm/utils/polling/index.js +6 -2
  42. package/dist/esm/utils/request/errors.js +4 -6
  43. package/dist/esm/utils/request/helpers.js +8 -3
  44. package/dist/types/client/file-fetcher/error.d.ts +9 -12
  45. package/dist/types/client/media-store/error.d.ts +3 -4
  46. package/dist/types/index.d.ts +1 -1
  47. package/dist/types/models/errors/index.d.ts +17 -4
  48. package/dist/types/models/errors/types.d.ts +7 -0
  49. package/dist/types/uploader/error.d.ts +8 -10
  50. package/dist/types/utils/mobileUpload/error.d.ts +9 -12
  51. package/dist/types/utils/polling/errors.d.ts +4 -5
  52. package/dist/types/utils/polling/types.d.ts +3 -0
  53. package/dist/types/utils/request/errors.d.ts +3 -5
  54. package/dist/types-ts4.5/client/file-fetcher/error.d.ts +9 -12
  55. package/dist/types-ts4.5/client/media-store/error.d.ts +3 -4
  56. package/dist/types-ts4.5/index.d.ts +1 -1
  57. package/dist/types-ts4.5/models/errors/index.d.ts +17 -4
  58. package/dist/types-ts4.5/models/errors/types.d.ts +7 -0
  59. package/dist/types-ts4.5/uploader/error.d.ts +8 -10
  60. package/dist/types-ts4.5/utils/mobileUpload/error.d.ts +9 -12
  61. package/dist/types-ts4.5/utils/polling/errors.d.ts +4 -5
  62. package/dist/types-ts4.5/utils/polling/types.d.ts +3 -0
  63. package/dist/types-ts4.5/utils/request/errors.d.ts +3 -5
  64. package/package.json +2 -2
@@ -27,36 +27,27 @@ import { isEmptyFile } from '../../utils/detectEmptyFile';
27
27
  import { mediaStore } from '@atlaskit/media-state';
28
28
  import { createCopyIntentRegisterationBatcher } from '../../utils/createCopyIntentRegisterationBatcher';
29
29
  import { defaultShouldRetryError } from '../../utils/request/helpers';
30
+ import { isCommonMediaClientError, CommonMediaClientError, fromCommonMediaClientError } from '../../models/errors';
30
31
  export { isFileFetcherError, FileFetcherError } from './error';
31
32
  const isCopySourceFileWithToken = token => !!token.authProvider;
32
33
  const isCopyDestinationWithToken = token => !!token.authProvider;
33
34
  export class FileFetcherImpl {
34
35
  constructor(mediaApi, store = mediaStore) {
35
36
  _defineProperty(this, "getErrorFileState", (error, id, occurrenceKey) => {
36
- var _error$attributes;
37
- const {
38
- metadata,
39
- ...attributes
40
- } = (_error$attributes = error === null || error === void 0 ? void 0 : error.attributes) !== null && _error$attributes !== void 0 ? _error$attributes : {};
41
- return typeof error === 'string' ? {
42
- status: 'error',
43
- id,
44
- reason: error,
45
- occurrenceKey,
46
- message: error
47
- } : {
48
- status: 'error',
49
- id,
50
- reason: error === null || error === void 0 ? void 0 : error.reason,
51
- details: {
52
- ...attributes,
53
- ...((metadata === null || metadata === void 0 ? void 0 : metadata.traceContext) && {
54
- metadata
55
- })
56
- },
57
- occurrenceKey,
58
- message: error === null || error === void 0 ? void 0 : error.message
59
- };
37
+ if (isCommonMediaClientError(error)) {
38
+ return fromCommonMediaClientError(id, occurrenceKey, error);
39
+ }
40
+ // ________________________________________________
41
+ // Legacy serializers
42
+ // We need to revisit all the incoming error types and ensure all the "reason" values are known
43
+ // We need to change the input error: any for a known type.
44
+ if (typeof error === 'string') {
45
+ const err = new CommonMediaClientError(error);
46
+ return fromCommonMediaClientError(id, occurrenceKey, err);
47
+ } else {
48
+ const err = new CommonMediaClientError((error === null || error === void 0 ? void 0 : error.reason) || 'unknown', error === null || error === void 0 ? void 0 : error.metadata, error === null || error === void 0 ? void 0 : error.innerError);
49
+ return fromCommonMediaClientError(id, occurrenceKey, err);
50
+ }
60
51
  });
61
52
  _defineProperty(this, "setFileState", (id, fileState) => {
62
53
  this.store.setState(state => {
@@ -77,14 +68,16 @@ export class FileFetcherImpl {
77
68
  includeHashForDuplicateFiles
78
69
  });
79
70
  if (isNotFoundMediaItemDetails(response)) {
80
- throw new FileFetcherError('emptyItems', id, {
71
+ throw new FileFetcherError('emptyItems', {
72
+ id,
81
73
  collectionName,
82
74
  occurrenceKey,
83
75
  traceContext: response.metadataTraceContext
84
76
  });
85
77
  }
86
78
  if (isEmptyFile(response)) {
87
- throw new FileFetcherError('zeroVersionFile', id, {
79
+ throw new FileFetcherError('zeroVersionFile', {
80
+ id,
88
81
  collectionName,
89
82
  occurrenceKey,
90
83
  traceContext: response.metadataTraceContext
@@ -152,29 +145,13 @@ export class FileFetcherImpl {
152
145
  includeHashForDuplicateFiles
153
146
  } = options;
154
147
  if (!isValidId(id)) {
155
- var _err$attributes;
156
148
  const subject = createMediaSubject();
157
- const err = new FileFetcherError('invalidFileId', id, {
149
+ const err = new FileFetcherError('invalidFileId', {
150
+ id,
158
151
  collectionName,
159
152
  occurrenceKey
160
153
  });
161
- const {
162
- metadata,
163
- ...attributes
164
- } = (_err$attributes = err === null || err === void 0 ? void 0 : err.attributes) !== null && _err$attributes !== void 0 ? _err$attributes : {};
165
- const errorFileState = {
166
- status: 'error',
167
- id,
168
- reason: err === null || err === void 0 ? void 0 : err.reason,
169
- message: err === null || err === void 0 ? void 0 : err.message,
170
- occurrenceKey,
171
- details: {
172
- ...attributes,
173
- ...((metadata === null || metadata === void 0 ? void 0 : metadata.traceContext) && {
174
- metadata
175
- })
176
- }
177
- };
154
+ const errorFileState = this.getErrorFileState(err, id, occurrenceKey);
178
155
  subject.error(err);
179
156
  this.setFileState(id, errorFileState);
180
157
  return fromObservable(subject);
@@ -1,10 +1,11 @@
1
1
  import { BaseMediaClientError } from '../../models/errors';
2
2
  export class MediaStoreError extends BaseMediaClientError {
3
3
  constructor(reason, innerError) {
4
- super(reason);
5
- this.reason = reason;
6
- this.innerError = innerError;
4
+ super(reason, undefined, innerError);
7
5
  }
6
+
7
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
8
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
8
9
  get attributes() {
9
10
  const {
10
11
  reason,
@@ -2,7 +2,7 @@ export { MediaStore, MediaStoreError, isMediaStoreError, getMediaEnvironment, ge
2
2
  export { UploadController } from './upload-controller';
3
3
  export { isPreviewableType } from './models/media';
4
4
  export { getArtifactUrl } from './models/artifacts';
5
- export { isMediaClientError, getMediaClientErrorReason } from './models/errors';
5
+ export { isMediaClientError, getMediaClientErrorReason, isCommonMediaClientError, toCommonMediaClientError } from './models/errors';
6
6
  export { isUploadingFileState, isProcessingFileState, isProcessedFileState, isErrorFileState, isPreviewableFileState, isFinalFileState, isImageRepresentationReady, mapMediaFileToFileState, mapMediaItemToFileState } from './models/file-state';
7
7
  export { uploadFile } from './uploader';
8
8
  export { request, RequestError, isRequestError, isRateLimitedError, createRequestErrorReason } from './utils/request';
@@ -3,12 +3,16 @@ export { isMediaClientError, getMediaClientErrorReason } from './helpers';
3
3
  /**
4
4
  * Base class for media errors
5
5
  */
6
- export class BaseMediaClientError extends Error {
7
- constructor(message) {
8
- super(message);
6
+ export class BaseMediaClientError extends Error
7
+ // TODO: Deprecate attributes getter https://product-fabric.atlassian.net/browse/CXP-4665
8
+ {
9
+ constructor(reason, metadata, innerError) {
10
+ super(reason);
9
11
 
10
12
  // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
11
- this.message = message;
13
+ this.reason = reason;
14
+ this.metadata = metadata;
15
+ this.innerError = innerError;
12
16
  Object.setPrototypeOf(this, new.target.prototype);
13
17
 
14
18
  // https://v8.dev/docs/stack-trace-api
@@ -16,4 +20,57 @@ export class BaseMediaClientError extends Error {
16
20
  Error.captureStackTrace(this, new.target);
17
21
  }
18
22
  }
19
- }
23
+
24
+ // TODO: Deprecate attributes getter https://product-fabric.atlassian.net/browse/CXP-4665
25
+ }
26
+
27
+ // The only reason why this class exists is because BaseMediaClientError is abstract class, so it can't be used to deserialize ErrorFileState. We can refactor and simplify this later.
28
+ /** Generic Media Client Erorr. All errors extending BaseMediaClientError match CommonMediaClientError attributes. Used to deserialize ErrorFileState */
29
+ export class CommonMediaClientError extends BaseMediaClientError {
30
+ constructor(reason, metadata, innerError) {
31
+ super(reason, metadata, innerError);
32
+ }
33
+ get attributes() {
34
+ return {
35
+ reason: this.reason
36
+ };
37
+ }
38
+ }
39
+ export function isCommonMediaClientError(error) {
40
+ if (!error) {
41
+ return false;
42
+ }
43
+ // Check if the error is an instance of Error
44
+ if (error instanceof CommonMediaClientError) {
45
+ return true;
46
+ }
47
+ return typeof error.reason === 'string' && ('metadata' in error || error.metadata === undefined) && ('innerError' in error || error.innerError === undefined);
48
+ }
49
+
50
+ /** Deserializer ErrorFileState -> CommonMediaClientError */
51
+ export const toCommonMediaClientError = errorFileState => {
52
+ var _errorFileState$detai;
53
+ const error = (_errorFileState$detai = errorFileState.details) === null || _errorFileState$detai === void 0 ? void 0 : _errorFileState$detai.error;
54
+ return new CommonMediaClientError((error === null || error === void 0 ? void 0 : error.reason) || 'unknown-reason', error.metadata, error.innerError);
55
+ };
56
+
57
+ /** Serializer CommonMediaClientError -> ErrorFileState */
58
+ export const fromCommonMediaClientError = (id, occurrenceKey, error) => {
59
+ return {
60
+ status: 'error',
61
+ id,
62
+ occurrenceKey,
63
+ reason: error.reason,
64
+ details: {
65
+ /** Use this attr to translate back into CommonMediaClientError (toCommonMediaClientError) */
66
+ error: {
67
+ reason: error === null || error === void 0 ? void 0 : error.reason,
68
+ metadata: error === null || error === void 0 ? void 0 : error.metadata,
69
+ innerError: error === null || error === void 0 ? void 0 : error.innerError
70
+ },
71
+ // Legacy details
72
+ ...(error === null || error === void 0 ? void 0 : error.attributes)
73
+ },
74
+ message: error === null || error === void 0 ? void 0 : error.message
75
+ };
76
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,19 +1,22 @@
1
1
  import { BaseMediaClientError } from '../models/errors';
2
2
  export class UploaderError extends BaseMediaClientError {
3
- constructor(reason, id, metadata) {
4
- super(reason);
5
- this.reason = reason;
6
- this.id = id;
7
- this.metadata = metadata;
3
+ // Legacy Attribute. Should be removed
4
+
5
+ constructor(reason, metadata) {
6
+ super(reason, metadata, undefined);
7
+ this.id = metadata.id;
8
8
  }
9
+
10
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
11
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
9
12
  get attributes() {
10
13
  const {
11
14
  reason,
12
- id,
13
15
  metadata: {
16
+ id,
14
17
  collectionName,
15
18
  occurrenceKey
16
- } = {}
19
+ }
17
20
  } = this;
18
21
  return {
19
22
  reason,
@@ -70,7 +70,8 @@ export const uploadFile = (file, store, uploadableFileUpfrontIds, callbacks, tra
70
70
  }
71
71
  } catch (err) {
72
72
  if (err instanceof Error && err.message === fileSizeError) {
73
- callbacks === null || callbacks === void 0 ? void 0 : callbacks.onUploadFinish(new UploaderError(err.message, id, {
73
+ callbacks === null || callbacks === void 0 ? void 0 : callbacks.onUploadFinish(new UploaderError(err.message, {
74
+ id,
74
75
  collectionName: collection,
75
76
  occurrenceKey: occurrenceKey
76
77
  }));
@@ -1,20 +1,23 @@
1
1
  import { BaseMediaClientError } from '../../models/errors';
2
2
  export class MobileUploadError extends BaseMediaClientError {
3
- constructor(reason, id, metadata) {
4
- super(reason);
5
- this.reason = reason;
6
- this.id = id;
7
- this.metadata = metadata;
3
+ // Legacy Attribute. Should be removed
4
+
5
+ constructor(reason, metadata) {
6
+ super(reason, metadata, undefined);
7
+ this.id = metadata.id;
8
8
  }
9
+
10
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
11
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
9
12
  get attributes() {
10
13
  const {
11
14
  reason,
12
- id,
13
15
  metadata: {
16
+ id,
14
17
  collectionName,
15
18
  occurrenceKey,
16
19
  traceContext
17
- } = {}
20
+ }
18
21
  } = this;
19
22
  return {
20
23
  reason,
@@ -24,14 +24,16 @@ export const createMobileDownloadFileStream = (dataloader, id, collectionName, o
24
24
  collectionName
25
25
  });
26
26
  if (isNotFoundMediaItemDetails(response)) {
27
- throw new MobileUploadError('emptyItems', id, {
27
+ throw new MobileUploadError('emptyItems', {
28
+ id,
28
29
  collectionName,
29
30
  occurrenceKey,
30
31
  traceContext: response.metadataTraceContext
31
32
  });
32
33
  }
33
34
  if (isEmptyFile(response)) {
34
- throw new MobileUploadError('zeroVersionFile', id, {
35
+ throw new MobileUploadError('zeroVersionFile', {
36
+ id,
35
37
  collectionName,
36
38
  occurrenceKey,
37
39
  traceContext: response.metadataTraceContext
@@ -1,14 +1,17 @@
1
1
  import { BaseMediaClientError } from '../../models/errors';
2
2
  export class PollingError extends BaseMediaClientError {
3
- constructor(reason, attempts) {
4
- super(reason);
5
- this.reason = reason;
6
- this.attempts = attempts;
3
+ constructor(reason, metadata) {
4
+ super(reason, metadata, undefined);
7
5
  }
6
+
7
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
8
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
8
9
  get attributes() {
9
10
  const {
10
11
  reason,
11
- attempts
12
+ metadata: {
13
+ attempts
14
+ }
12
15
  } = this;
13
16
  return {
14
17
  reason,
@@ -38,7 +38,9 @@ export class PollingFunction {
38
38
  } = this.options;
39
39
  if (poll_maxAttempts === 0) {
40
40
  // hard kill, polling disabled
41
- return this.fail(new PollingError('pollingMaxAttemptsExceeded', this.attempt));
41
+ return this.fail(new PollingError('pollingMaxAttemptsExceeded', {
42
+ attempts: this.attempt
43
+ }));
42
44
  }
43
45
  try {
44
46
  // executor must explicitly call this.next() for triggering next iteration (pull)
@@ -49,7 +51,9 @@ export class PollingFunction {
49
51
  }
50
52
  if (this.attempt >= poll_maxAttempts) {
51
53
  // max iterations exceeded, let the consumer know
52
- return this.fail(new PollingError('pollingMaxAttemptsExceeded', this.attempt));
54
+ return this.fail(new PollingError('pollingMaxAttemptsExceeded', {
55
+ attempts: this.attempt
56
+ }));
53
57
  }
54
58
  this.poll_intervalMs = this.getIntervalMsForIteration(this.attempt);
55
59
  this.attempt++;
@@ -1,11 +1,11 @@
1
1
  import { BaseMediaClientError } from '../../models/errors';
2
2
  export class RequestError extends BaseMediaClientError {
3
3
  constructor(reason, metadata, innerError) {
4
- super(reason);
5
- this.reason = reason;
6
- this.metadata = metadata;
7
- this.innerError = innerError;
4
+ super(reason, metadata, innerError);
8
5
  }
6
+
7
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
8
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
9
9
  get attributes() {
10
10
  const {
11
11
  reason,
@@ -2,6 +2,10 @@ import { isClientBasedAuth } from '@atlaskit/media-core';
2
2
  import { getRandomHex } from '@atlaskit/media-common';
3
3
  import { mapAuthToQueryParameters } from '../../models/auth-query-parameters';
4
4
  import { RequestError, isRequestError } from './errors';
5
+ const getStatusCode = error => {
6
+ var _error$metadata;
7
+ return isRequestError(error) && ((_error$metadata = error.metadata) === null || _error$metadata === void 0 ? void 0 : _error$metadata.statusCode) && error.metadata.statusCode;
8
+ };
5
9
  export function waitPromise(timeout) {
6
10
  return new Promise(resolve => setTimeout(resolve, timeout));
7
11
  }
@@ -14,7 +18,8 @@ export function isFetchNetworkError(err) {
14
18
  return err instanceof TypeError;
15
19
  }
16
20
  export function isRateLimitedError(error) {
17
- return !!error && isRequestError(error) && error.attributes.statusCode === 429 || !!error && !!error.message && error.message.includes('429');
21
+ const statusCode = error && getStatusCode(error);
22
+ return statusCode === 429 || !!error && !!error.message && error.message.includes('429');
18
23
  }
19
24
  export const extendTraceContext = traceContext => traceContext ? {
20
25
  ...traceContext,
@@ -101,8 +106,8 @@ export function createMapResponseToBlob(metadata) {
101
106
  };
102
107
  }
103
108
  export const defaultShouldRetryError = err => {
104
- var _err$metadata;
105
- return isFetchNetworkError(err) || isRequestError(err) && !!(err !== null && err !== void 0 && (_err$metadata = err.metadata) !== null && _err$metadata !== void 0 && _err$metadata.statusCode) && err.metadata.statusCode >= 500;
109
+ const statusCode = getStatusCode(err);
110
+ return isFetchNetworkError(err) || (statusCode ? statusCode >= 500 : false);
106
111
  };
107
112
  export const DEFAULT_RETRY_OPTIONS = {
108
113
  startTimeoutInMs: 1000,
@@ -10,26 +10,28 @@ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstruct
10
10
  function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
11
11
  import { BaseMediaClientError } from '../../models/errors';
12
12
  export var FileFetcherError = /*#__PURE__*/function (_BaseMediaClientError) {
13
- function FileFetcherError(reason, id, metadata) {
13
+ // Legacy Attribute. Should be removed
14
+
15
+ function FileFetcherError(reason, metadata) {
14
16
  var _this;
15
17
  _classCallCheck(this, FileFetcherError);
16
- _this = _callSuper(this, FileFetcherError, [reason]);
17
- _this.reason = reason;
18
- _this.id = id;
19
- _this.metadata = metadata;
18
+ _this = _callSuper(this, FileFetcherError, [reason, metadata, undefined]);
19
+ _this.id = metadata.id;
20
20
  return _this;
21
21
  }
22
+
23
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
24
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
22
25
  _inherits(FileFetcherError, _BaseMediaClientError);
23
26
  return _createClass(FileFetcherError, [{
24
27
  key: "attributes",
25
28
  get: function get() {
26
29
  var reason = this.reason,
27
- id = this.id,
28
30
  _this$metadata = this.metadata,
29
- _this$metadata2 = _this$metadata === void 0 ? {} : _this$metadata,
30
- collectionName = _this$metadata2.collectionName,
31
- occurrenceKey = _this$metadata2.occurrenceKey,
32
- traceContext = _this$metadata2.traceContext;
31
+ id = _this$metadata.id,
32
+ collectionName = _this$metadata.collectionName,
33
+ occurrenceKey = _this$metadata.occurrenceKey,
34
+ traceContext = _this$metadata.traceContext;
33
35
  return _objectSpread({
34
36
  reason: reason,
35
37
  id: id,
@@ -1,13 +1,10 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
2
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
3
  import _createClass from "@babel/runtime/helpers/createClass";
5
4
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
6
- var _excluded = ["metadata"],
7
- _excluded2 = ["metadata"];
8
- import _regeneratorRuntime from "@babel/runtime/regenerator";
9
5
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
10
6
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
11
8
  import { Subscription } from 'rxjs/Subscription';
12
9
  import { map } from 'rxjs/operators/map';
13
10
  import uuid from 'uuid/v4';
@@ -36,6 +33,7 @@ import { isEmptyFile } from '../../utils/detectEmptyFile';
36
33
  import { mediaStore } from '@atlaskit/media-state';
37
34
  import { createCopyIntentRegisterationBatcher } from '../../utils/createCopyIntentRegisterationBatcher';
38
35
  import { defaultShouldRetryError } from '../../utils/request/helpers';
36
+ import { isCommonMediaClientError, CommonMediaClientError, fromCommonMediaClientError } from '../../models/errors';
39
37
  export { isFileFetcherError, FileFetcherError } from './error';
40
38
  var isCopySourceFileWithToken = function isCopySourceFileWithToken(token) {
41
39
  return !!token.authProvider;
@@ -49,26 +47,20 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
49
47
  var store = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : mediaStore;
50
48
  _classCallCheck(this, FileFetcherImpl);
51
49
  _defineProperty(this, "getErrorFileState", function (error, id, occurrenceKey) {
52
- var _error$attributes;
53
- var _ref = (_error$attributes = error === null || error === void 0 ? void 0 : error.attributes) !== null && _error$attributes !== void 0 ? _error$attributes : {},
54
- metadata = _ref.metadata,
55
- attributes = _objectWithoutProperties(_ref, _excluded);
56
- return typeof error === 'string' ? {
57
- status: 'error',
58
- id: id,
59
- reason: error,
60
- occurrenceKey: occurrenceKey,
61
- message: error
62
- } : {
63
- status: 'error',
64
- id: id,
65
- reason: error === null || error === void 0 ? void 0 : error.reason,
66
- details: _objectSpread(_objectSpread({}, attributes), (metadata === null || metadata === void 0 ? void 0 : metadata.traceContext) && {
67
- metadata: metadata
68
- }),
69
- occurrenceKey: occurrenceKey,
70
- message: error === null || error === void 0 ? void 0 : error.message
71
- };
50
+ if (isCommonMediaClientError(error)) {
51
+ return fromCommonMediaClientError(id, occurrenceKey, error);
52
+ }
53
+ // ________________________________________________
54
+ // Legacy serializers
55
+ // We need to revisit all the incoming error types and ensure all the "reason" values are known
56
+ // We need to change the input error: any for a known type.
57
+ if (typeof error === 'string') {
58
+ var err = new CommonMediaClientError(error);
59
+ return fromCommonMediaClientError(id, occurrenceKey, err);
60
+ } else {
61
+ var _err = new CommonMediaClientError((error === null || error === void 0 ? void 0 : error.reason) || 'unknown', error === null || error === void 0 ? void 0 : error.metadata, error === null || error === void 0 ? void 0 : error.innerError);
62
+ return fromCommonMediaClientError(id, occurrenceKey, _err);
63
+ }
72
64
  });
73
65
  _defineProperty(this, "setFileState", function (id, fileState) {
74
66
  _this.store.setState(function (state) {
@@ -101,7 +93,8 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
101
93
  _context.next = 5;
102
94
  break;
103
95
  }
104
- throw new FileFetcherError('emptyItems', id, {
96
+ throw new FileFetcherError('emptyItems', {
97
+ id: id,
105
98
  collectionName: collectionName,
106
99
  occurrenceKey: occurrenceKey,
107
100
  traceContext: response.metadataTraceContext
@@ -111,7 +104,8 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
111
104
  _context.next = 7;
112
105
  break;
113
106
  }
114
- throw new FileFetcherError('zeroVersionFile', id, {
107
+ throw new FileFetcherError('zeroVersionFile', {
108
+ id: id,
115
109
  collectionName: collectionName,
116
110
  occurrenceKey: occurrenceKey,
117
111
  traceContext: response.metadataTraceContext
@@ -184,25 +178,13 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
184
178
  occurrenceKey = options.occurrenceKey,
185
179
  includeHashForDuplicateFiles = options.includeHashForDuplicateFiles;
186
180
  if (!isValidId(id)) {
187
- var _err$attributes;
188
181
  var subject = createMediaSubject();
189
- var err = new FileFetcherError('invalidFileId', id, {
182
+ var err = new FileFetcherError('invalidFileId', {
183
+ id: id,
190
184
  collectionName: collectionName,
191
185
  occurrenceKey: occurrenceKey
192
186
  });
193
- var _ref3 = (_err$attributes = err === null || err === void 0 ? void 0 : err.attributes) !== null && _err$attributes !== void 0 ? _err$attributes : {},
194
- metadata = _ref3.metadata,
195
- attributes = _objectWithoutProperties(_ref3, _excluded2);
196
- var errorFileState = {
197
- status: 'error',
198
- id: id,
199
- reason: err === null || err === void 0 ? void 0 : err.reason,
200
- message: err === null || err === void 0 ? void 0 : err.message,
201
- occurrenceKey: occurrenceKey,
202
- details: _objectSpread(_objectSpread({}, attributes), (metadata === null || metadata === void 0 ? void 0 : metadata.traceContext) && {
203
- metadata: metadata
204
- })
205
- };
187
+ var errorFileState = this.getErrorFileState(err, id, occurrenceKey);
206
188
  subject.error(err);
207
189
  this.setFileState(id, errorFileState);
208
190
  return fromObservable(subject);
@@ -243,8 +225,8 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
243
225
  descriptors: descriptors
244
226
  }, {
245
227
  collection: collection
246
- }, traceContext).then(function (_ref4) {
247
- var data = _ref4.data;
228
+ }, traceContext).then(function (_ref2) {
229
+ var data = _ref2.data;
248
230
  return data;
249
231
  });
250
232
  }
@@ -285,7 +267,7 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
285
267
  return undefined;
286
268
  });
287
269
  preview = new Promise( /*#__PURE__*/function () {
288
- var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(resolve, reject) {
270
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(resolve, reject) {
289
271
  var blob;
290
272
  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
291
273
  while (1) switch (_context2.prev = _context2.next) {
@@ -308,7 +290,7 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
308
290
  }, _callee2);
309
291
  }));
310
292
  return function (_x4, _x5) {
311
- return _ref5.apply(this, arguments);
293
+ return _ref3.apply(this, arguments);
312
294
  };
313
295
  }());
314
296
  name = url.split('/').pop() || ''; // we create a initial fileState with the minimum info that we have at this point
@@ -327,7 +309,7 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
327
309
  getFileStreamsCache().set(id, subject);
328
310
  this.setFileState(id, fileState);
329
311
  return _context4.abrupt("return", new Promise( /*#__PURE__*/function () {
330
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(resolve, reject) {
312
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(resolve, reject) {
331
313
  var blob, type, size, file, mediaType, dimensions;
332
314
  return _regeneratorRuntime.wrap(function _callee3$(_context3) {
333
315
  while (1) switch (_context3.prev = _context3.next) {
@@ -388,7 +370,7 @@ export var FileFetcherImpl = /*#__PURE__*/function () {
388
370
  }, _callee3, null, [[10, 16]]);
389
371
  }));
390
372
  return function (_x6, _x7) {
391
- return _ref6.apply(this, arguments);
373
+ return _ref4.apply(this, arguments);
392
374
  };
393
375
  }()));
394
376
  case 11:
@@ -8,13 +8,12 @@ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.
8
8
  import { BaseMediaClientError } from '../../models/errors';
9
9
  export var MediaStoreError = /*#__PURE__*/function (_BaseMediaClientError) {
10
10
  function MediaStoreError(reason, innerError) {
11
- var _this;
12
11
  _classCallCheck(this, MediaStoreError);
13
- _this = _callSuper(this, MediaStoreError, [reason]);
14
- _this.reason = reason;
15
- _this.innerError = innerError;
16
- return _this;
12
+ return _callSuper(this, MediaStoreError, [reason, undefined, innerError]);
17
13
  }
14
+
15
+ // TODO: Deprecate this getter https://product-fabric.atlassian.net/browse/CXP-4665
16
+ /** Will be deprecated. Use the properties `reason` and `metadata` instead */
18
17
  _inherits(MediaStoreError, _BaseMediaClientError);
19
18
  return _createClass(MediaStoreError, [{
20
19
  key: "attributes",
package/dist/esm/index.js CHANGED
@@ -2,7 +2,7 @@ export { MediaStore, MediaStoreError, isMediaStoreError, getMediaEnvironment, ge
2
2
  export { UploadController } from './upload-controller';
3
3
  export { isPreviewableType } from './models/media';
4
4
  export { getArtifactUrl } from './models/artifacts';
5
- export { isMediaClientError, getMediaClientErrorReason } from './models/errors';
5
+ export { isMediaClientError, getMediaClientErrorReason, isCommonMediaClientError, toCommonMediaClientError } from './models/errors';
6
6
  export { isUploadingFileState, isProcessingFileState, isProcessedFileState, isErrorFileState, isPreviewableFileState, isFinalFileState, isImageRepresentationReady, mapMediaFileToFileState, mapMediaItemToFileState } from './models/file-state';
7
7
  export { uploadFile } from './uploader';
8
8
  export { request, RequestError, isRequestError, isRateLimitedError, createRequestErrorReason } from './utils/request';