Sfoglia il codice sorgente

feat(ts) migrate JitsiTrackError to TS

dev0
Naman Jain 6 mesi fa
parent
commit
6997701f69
Nessun account collegato all'indirizzo email del committer
2 ha cambiato i file con 158 aggiunte e 164 eliminazioni
  1. 0
    164
      JitsiTrackError.js
  2. 158
    0
      JitsiTrackError.ts

+ 0
- 164
JitsiTrackError.js Vedi File

@@ -1,164 +0,0 @@
1
-import * as JitsiTrackErrors from './JitsiTrackErrors';
2
-
3
-const TRACK_ERROR_TO_MESSAGE_MAP = {};
4
-
5
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.UNSUPPORTED_RESOLUTION]
6
-    = 'Video resolution is not supported: ';
7
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.SCREENSHARING_USER_CANCELED]
8
-    = 'User canceled screen sharing prompt';
9
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.SCREENSHARING_GENERIC_ERROR]
10
-    = 'Unknown error from screensharing';
11
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.SCREENSHARING_NOT_SUPPORTED_ERROR]
12
-    = 'Not supported';
13
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR]
14
-    = 'Unkown error from desktop picker';
15
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]
16
-    = 'Failed to detect desktop picker';
17
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL]
18
-    = 'Generic getUserMedia error';
19
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.PERMISSION_DENIED]
20
-    = 'User denied permission to use device(s): ';
21
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NOT_FOUND]
22
-    = 'Requested device(s) was/were not found: ';
23
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CONSTRAINT_FAILED]
24
-    = 'Constraint could not be satisfied: ';
25
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TIMEOUT]
26
-    = 'Could not start media source. Timeout occurred!';
27
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_IS_DISPOSED]
28
-    = 'Track has been already disposed';
29
-TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_NO_STREAM_FOUND]
30
-    = 'Track does not have an associated Media Stream';
31
-
32
-// FIXME: Using prototype inheritance because otherwise instanceof is not
33
-// working properly (see https://github.com/babel/babel/issues/3083)
34
-
35
-/**
36
- *
37
- * Represents an error that occurred to a JitsiTrack. Can represent various
38
- * types of errors. For error descriptions (@see JitsiTrackErrors).
39
- *
40
- * @extends Error
41
- *
42
- *
43
- * @constructor
44
- * @param {Object|string} error - error object or error name
45
- * @param {Object|string} (options) - getUserMedia constraints object or
46
- * error message
47
- * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
48
- * list of getUserMedia requested devices
49
- */
50
-function JitsiTrackError(error, options, devices) {
51
-    if (typeof error === 'object' && typeof error.name !== 'undefined') {
52
-        /**
53
-         * Additional information about original getUserMedia error
54
-         * and constraints.
55
-         * @type {{
56
-         *     error: Object,
57
-         *     constraints: Object,
58
-         *     devices: Array.<'audio'|'video'|'desktop'|'screen'>
59
-         * }}
60
-         */
61
-        this.gum = {
62
-            error,
63
-            constraints: options,
64
-            devices: devices && Array.isArray(devices)
65
-                ? devices.slice(0)
66
-                : undefined
67
-        };
68
-
69
-        switch (error.name) {
70
-        case 'NotAllowedError':
71
-        case 'PermissionDeniedError':
72
-        case 'SecurityError':
73
-            this.name = JitsiTrackErrors.PERMISSION_DENIED;
74
-            this.message
75
-                = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
76
-                    + (this.gum.devices || []).join(', ');
77
-            break;
78
-        case 'DevicesNotFoundError':
79
-        case 'NotFoundError':
80
-            this.name = JitsiTrackErrors.NOT_FOUND;
81
-            this.message
82
-                = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
83
-                    + (this.gum.devices || []).join(', ');
84
-            break;
85
-        case 'ConstraintNotSatisfiedError':
86
-        case 'OverconstrainedError': {
87
-            const constraintName = error.constraintName || error.constraint;
88
-
89
-            // we treat deviceId as unsupported resolution, as we want to
90
-            // retry and finally if everything fails to remove deviceId from
91
-            // mandatory constraints
92
-            if (options
93
-                    && options.video
94
-                    && (!devices || devices.indexOf('video') > -1)
95
-                    && (constraintName === 'minWidth'
96
-                        || constraintName === 'maxWidth'
97
-                        || constraintName === 'minHeight'
98
-                        || constraintName === 'maxHeight'
99
-                        || constraintName === 'width'
100
-                        || constraintName === 'height'
101
-                        || constraintName === 'deviceId')) {
102
-                this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
103
-                this.message
104
-                    = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
105
-                        + getResolutionFromFailedConstraint(
106
-                            constraintName,
107
-                            options);
108
-            } else {
109
-                this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
110
-                this.message
111
-                    = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
112
-                        + error.constraintName;
113
-            }
114
-            break;
115
-        }
116
-
117
-        default:
118
-            this.name = JitsiTrackErrors.GENERAL;
119
-            this.message
120
-                = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
121
-            break;
122
-        }
123
-    } else if (typeof error === 'string') {
124
-        if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
125
-            this.name = error;
126
-            this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
127
-        } else {
128
-            // this is some generic error that do not fit any of our
129
-            // pre-defined errors, so don't give it any specific name, just
130
-            // store message
131
-            this.message = error;
132
-        }
133
-    } else {
134
-        throw new Error('Invalid arguments');
135
-    }
136
-
137
-    this.stack = error.stack || new Error().stack;
138
-}
139
-
140
-JitsiTrackError.prototype = Object.create(Error.prototype);
141
-JitsiTrackError.prototype.constructor = JitsiTrackError;
142
-
143
-/**
144
- * Gets failed resolution constraint from corresponding object.
145
- * @param {string} failedConstraintName
146
- * @param {Object} constraints
147
- * @returns {string|number}
148
- */
149
-function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
150
-    if (constraints && constraints.video && constraints.video.mandatory) {
151
-        switch (failedConstraintName) {
152
-        case 'width':
153
-            return constraints.video.mandatory.minWidth;
154
-        case 'height':
155
-            return constraints.video.mandatory.minHeight;
156
-        default:
157
-            return constraints.video.mandatory[failedConstraintName] || '';
158
-        }
159
-    }
160
-
161
-    return '';
162
-}
163
-
164
-export default JitsiTrackError;

+ 158
- 0
JitsiTrackError.ts Vedi File

@@ -0,0 +1,158 @@
1
+import * as JitsiTrackErrors from './JitsiTrackErrors';
2
+
3
+export interface IGumError {
4
+    constraint?: string;
5
+    constraintName?: string;
6
+    message?: string;
7
+    name?: string;
8
+    stack?: string;
9
+}
10
+
11
+export interface IVideoConstraints {
12
+    mandatory?: { [key: string]: string | number; };
13
+}
14
+
15
+export interface IGumOptions {
16
+    video?: IVideoConstraints;
17
+}
18
+
19
+export interface IGum {
20
+    constraints?: IGumOptions | string;
21
+    devices?: ('audio' | 'video' | 'desktop' | 'screen' | 'audiooutput')[];
22
+    error: IGumError;
23
+}
24
+
25
+export type DeviceType = 'audio' | 'video' | 'desktop' | 'screen' | 'audiooutput';
26
+
27
+const TRACK_ERROR_TO_MESSAGE_MAP: { [key: string]: string; } = {
28
+    [JitsiTrackErrors.UNSUPPORTED_RESOLUTION]: 'Video resolution is not supported: ',
29
+    [JitsiTrackErrors.SCREENSHARING_USER_CANCELED]: 'User canceled screen sharing prompt',
30
+    [JitsiTrackErrors.SCREENSHARING_GENERIC_ERROR]: 'Unknown error from screensharing',
31
+    [JitsiTrackErrors.SCREENSHARING_NOT_SUPPORTED_ERROR]: 'Not supported',
32
+    [JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR]: 'Unkown error from desktop picker',
33
+    [JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]: 'Failed to detect desktop picker',
34
+    [JitsiTrackErrors.GENERAL]: 'Generic getUserMedia error',
35
+    [JitsiTrackErrors.PERMISSION_DENIED]: 'User denied permission to use device(s): ',
36
+    [JitsiTrackErrors.NOT_FOUND]: 'Requested device(s) was/were not found: ',
37
+    [JitsiTrackErrors.CONSTRAINT_FAILED]: 'Constraint could not be satisfied: ',
38
+    [JitsiTrackErrors.TIMEOUT]: 'Could not start media source. Timeout occurred!',
39
+    [JitsiTrackErrors.TRACK_IS_DISPOSED]: 'Track has been already disposed',
40
+    [JitsiTrackErrors.TRACK_NO_STREAM_FOUND]: 'Track does not have an associated Media Stream'
41
+};
42
+
43
+/**
44
+ *
45
+ * Represents an error that occurred to a JitsiTrack. Can represent various
46
+ * types of errors. For error descriptions (@see JitsiTrackErrors).
47
+ *
48
+ * @extends Error
49
+ */
50
+export default class JitsiTrackError extends Error {
51
+    public gum?: IGum;
52
+
53
+    /**
54
+     * @constructor
55
+     * @param {IGumError|string} error - error object or error name
56
+     * @param {IGumOptions|string} [options] - getUserMedia constraints object or error message
57
+     * @param {DeviceType[]} [devices] - list of getUserMedia requested devices
58
+     */
59
+    constructor(
60
+            error: IGumError | string,
61
+            options?: IGumOptions | string,
62
+            devices?: DeviceType[]
63
+    ) {
64
+        super();
65
+
66
+        if (typeof error === 'object' && typeof error.name !== 'undefined') {
67
+            /**
68
+             * Additional information about original getUserMedia error
69
+             * and constraints.
70
+             * @type {IGum}
71
+             */
72
+            this.gum = {
73
+                error,
74
+                constraints: options,
75
+                devices: devices && Array.isArray(devices) ? devices.slice(0) : undefined
76
+            };
77
+
78
+            switch (error.name) {
79
+            case 'NotAllowedError':
80
+            case 'PermissionDeniedError':
81
+            case 'SecurityError':
82
+                this.name = JitsiTrackErrors.PERMISSION_DENIED;
83
+                this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name] + (this.gum.devices || []).join(', ');
84
+                break;
85
+            case 'DevicesNotFoundError':
86
+            case 'NotFoundError':
87
+                this.name = JitsiTrackErrors.NOT_FOUND;
88
+                this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name] + (this.gum.devices || []).join(', ');
89
+                break;
90
+            case 'ConstraintNotSatisfiedError':
91
+            case 'OverconstrainedError': {
92
+                const constraintName = error.constraintName || error.constraint;
93
+
94
+                // we treat deviceId as unsupported resolution, as we want to
95
+                // retry and finally if everything fails to remove deviceId from
96
+                // mandatory constraints
97
+                if (typeof options !== 'string'
98
+                        && options?.video
99
+                        && (!devices || devices.indexOf('video') > -1)
100
+                        && (constraintName === 'minWidth'
101
+                            || constraintName === 'maxWidth'
102
+                            || constraintName === 'minHeight'
103
+                            || constraintName === 'maxHeight'
104
+                            || constraintName === 'width'
105
+                            || constraintName === 'height'
106
+                            || constraintName === 'deviceId')) {
107
+                    this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
108
+                    this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
109
+                            + this.getResolutionFromFailedConstraint(constraintName, options);
110
+                } else {
111
+                    this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
112
+                    this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name] + error.constraintName;
113
+                }
114
+                break;
115
+            }
116
+            default:
117
+                this.name = JitsiTrackErrors.GENERAL;
118
+                this.message = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
119
+                break;
120
+            }
121
+        } else if (typeof error === 'string') {
122
+            if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
123
+                this.name = error;
124
+                this.message = typeof options === 'string' ? options : TRACK_ERROR_TO_MESSAGE_MAP[error];
125
+            } else {
126
+            // this is some generic error that do not fit any of our
127
+            // pre-defined errors, so don't give it any specific name, just
128
+            // store message
129
+                this.message = error;
130
+            }
131
+        } else {
132
+            throw new Error('Invalid arguments');
133
+        }
134
+
135
+        this.stack = typeof error === 'string' ? new Error().stack : error.stack;
136
+    }
137
+
138
+    /**
139
+     * Gets failed resolution constraint from corresponding object.
140
+     * @param failedConstraintName - The name of the failed constraint
141
+     * @param constraints - The constraints object
142
+     * @returns The resolution value or empty string
143
+     */
144
+    private getResolutionFromFailedConstraint(failedConstraintName: string, constraints: IGumOptions): string | number {
145
+        if (constraints?.video?.mandatory) {
146
+            switch (failedConstraintName) {
147
+            case 'width':
148
+                return constraints.video.mandatory.minWidth;
149
+            case 'height':
150
+                return constraints.video.mandatory.minHeight;
151
+            default:
152
+                return constraints.video.mandatory[failedConstraintName] || '';
153
+            }
154
+        }
155
+
156
+        return '';
157
+    }
158
+}

Loading…
Annulla
Salva