Skip to content

Commit 31f78d8

Browse files
committed
change error handling
1 parent d8c1101 commit 31f78d8

File tree

3 files changed

+19
-74
lines changed

3 files changed

+19
-74
lines changed

src/resource.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ function findOriginalExc(exc: unknown) {
1515

1616
if ("error" in body) {
1717
const message = body.error.message as string;
18-
const name = body.error.name as string;
19-
const stack = body.error.stack as string;
20-
2118
const cause = body.error.cause;
2219

2320
const error = new Error(message, { cause });
2421

25-
error.name = name;
26-
error.stack = stack;
22+
if (body.error.name) {
23+
error.name = body.error.name;
24+
}
25+
26+
if (body.error.stack) {
27+
error.stack = body.error.stack;
28+
}
2729

2830
return error;
2931
}

src/util/error/VideomailError.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class VideomailError extends Error {
3737
public static readonly BROWSER_PROBLEM = "browser-problem";
3838
public static readonly WEBCAM_PROBLEM = "webcam-problem";
3939
public static readonly OVERCONSTRAINED = "OverconstrainedError";
40-
public static readonly NOT_FOUND_ERROR = "NotFoundError";
4140
public static readonly NOT_READABLE_ERROR = "NotReadableError";
4241
public static readonly SECURITY_ERROR = "SecurityError";
4342
public static readonly TRACK_START_ERROR = "TrackStartError";

src/util/error/createError.ts

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ interface ErrorParams {
1818
function createError(errorParams: ErrorParams) {
1919
const { exc, options } = errorParams;
2020

21-
let message = errorParams.message;
22-
let explanation = errorParams.explanation;
23-
2421
let err = errorParams.err;
25-
const classList = errorParams.classList ?? [];
2622

2723
if (!err && exc instanceof Error) {
2824
err = exc;
@@ -32,44 +28,17 @@ function createError(errorParams: ErrorParams) {
3228
return err;
3329
}
3430

35-
const audioEnabled = isAudioEnabled(options);
36-
const browser = getBrowser(options);
37-
38-
let errorCode;
39-
let errType;
31+
let message = errorParams.message;
32+
let explanation = errorParams.explanation;
4033

41-
// whole code is ugly because all browsers behave so differently :(
34+
const classList = errorParams.classList ?? [];
4235

43-
if (typeof err === "object") {
44-
if ("code" in err) {
45-
errorCode = err.code;
46-
}
36+
const audioEnabled = isAudioEnabled(options);
37+
const browser = getBrowser(options);
4738

48-
if (err.name === VideomailError.TRACK_START_ERROR) {
49-
errType = VideomailError.TRACK_START_ERROR;
50-
} else if (err.name === VideomailError.SECURITY_ERROR) {
51-
errType = VideomailError.SECURITY_ERROR;
52-
} else if (errorCode === 8 && err.name === VideomailError.NOT_FOUND_ERROR) {
53-
errType = VideomailError.NOT_FOUND_ERROR;
54-
} else if (errorCode === 35 || err.name === VideomailError.NOT_ALLOWED_ERROR) {
55-
// https://github.com/binarykitchen/videomail.io/issues/411
56-
errType = VideomailError.NOT_ALLOWED_ERROR;
57-
} else if (err.constructor.name === VideomailError.DOM_EXCEPTION) {
58-
if (err.name === VideomailError.NOT_READABLE_ERROR) {
59-
errType = VideomailError.NOT_READABLE_ERROR;
60-
} else {
61-
errType = VideomailError.DOM_EXCEPTION;
62-
}
63-
} else if (err.constructor.name === VideomailError.OVERCONSTRAINED) {
64-
errType = VideomailError.OVERCONSTRAINED;
65-
} else if (err.name) {
66-
errType = err.name;
67-
}
68-
} else {
69-
errType = err;
70-
}
39+
const errName = err?.name ?? err?.constructor.name;
7140

72-
switch (errType) {
41+
switch (errName) {
7342
case VideomailError.SECURITY_ERROR:
7443
message = "The operation was insecure";
7544
explanation = "Probably you have disallowed Cookies for this page?";
@@ -97,7 +66,6 @@ function createError(errorParams: ErrorParams) {
9766
message = "Source of your webcam cannot be accessed";
9867
explanation = "Probably it is locked from another process or has a hardware error.";
9968
break;
100-
case VideomailError.NOT_FOUND_ERROR:
10169
case "NO_DEVICES_FOUND":
10270
if (audioEnabled) {
10371
message = "No webcam nor microphone found";
@@ -180,32 +148,9 @@ function createError(errorParams: ErrorParams) {
180148
break;
181149

182150
case VideomailError.DOM_EXCEPTION:
183-
switch (errorCode) {
184-
case 8:
185-
message = "Requested webcam not found";
186-
explanation = "A webcam is needed but could not be found";
187-
classList.push(VideomailError.WEBCAM_PROBLEM);
188-
break;
189-
case 9: {
190-
const newUrl = `https:${window.location.href.substring(window.location.protocol.length)}`;
191-
message = "Security upgrade needed";
192-
explanation =
193-
`Click <a href="${newUrl}">here</a> to switch to HTTPs which is more safe ` +
194-
` and enables encrypted videomail transfers.`;
195-
classList.push(VideomailError.BROWSER_PROBLEM);
196-
break;
197-
}
198-
case 11:
199-
message = "Invalid State";
200-
explanation = "The object is in an invalid, unusable state";
201-
classList.push(VideomailError.BROWSER_PROBLEM);
202-
break;
203-
default:
204-
message = "DOM Exception";
205-
explanation = pretty(err);
206-
classList.push(VideomailError.BROWSER_PROBLEM);
207-
break;
208-
}
151+
message = "DOM Exception";
152+
explanation = pretty(err);
153+
209154
break;
210155

211156
/*
@@ -233,7 +178,6 @@ function createError(errorParams: ErrorParams) {
233178
* error objects can be prettified to undefined sometimes
234179
*/
235180
if (!explanation && originalExplanation) {
236-
// tried toString before but nah
237181
explanation = `Inspected: ${originalExplanation}`;
238182
}
239183

@@ -243,8 +187,8 @@ function createError(errorParams: ErrorParams) {
243187

244188
// for weird, undefined cases
245189
if (!message) {
246-
if (errType) {
247-
message = `${errType} (weird)`;
190+
if (errName) {
191+
message = `${errName} (weird)`;
248192
}
249193

250194
if (!explanation) {

0 commit comments

Comments
 (0)