Skip to content

Commit

Permalink
Update errors reference, take 1 (#26391)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Apr 25, 2023
1 parent c2cfa85 commit c495a1e
Show file tree
Hide file tree
Showing 21 changed files with 348 additions and 488 deletions.
6 changes: 6 additions & 0 deletions files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12251,8 +12251,14 @@
/en-US/docs/Web/JavaScript/Reference/Code_comments /en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Comments
/en-US/docs/Web/JavaScript/Reference/Data_types /en-US/docs/Web/JavaScript/Data_structures
/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features/The_legacy_Iterator_protocol /en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features
/en-US/docs/Web/JavaScript/Reference/Errors/Bad_octal /en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal
/en-US/docs/Web/JavaScript/Reference/Errors/Bad_return_or_yield /en-US/docs/Web/JavaScript/Reference/Errors/Bad_return
/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property /en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_type
/en-US/docs/Web/JavaScript/Reference/Errors/Dead_object https://blog.mozilla.org/addons/2012/09/12/what-does-cant-access-dead-object-mean/
/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_String_generics /en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features
/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_expression_closures /en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features
/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_toLocaleFormat /en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features
/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_formal_parameter /en-US/docs/Web/JavaScript/Reference/Errors/Missing_formal_parameter
/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope /en-US/docs/Web/JavaScript/Reference/Functions
/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode /en-US/docs/Web/JavaScript/Reference/Strict_mode
/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode/Transitioning_to_strict_mode /en-US/docs/Web/JavaScript/Reference/Strict_mode
Expand Down
28 changes: 1 addition & 27 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -105639,10 +105639,6 @@
"modified": "2020-04-16T11:52:48.784Z",
"contributors": ["fscholz", "PatrickKettner", "nmve", "nbp"]
},
"Web/JavaScript/Reference/Errors/Bad_octal": {
"modified": "2020-04-16T12:40:59.260Z",
"contributors": ["fscholz"]
},
"Web/JavaScript/Reference/Errors/Bad_radix": {
"modified": "2020-04-14T12:36:17.047Z",
"contributors": ["fscholz", "PatrickKettner", "nmve", "Sheppy"]
Expand All @@ -105651,7 +105647,7 @@
"modified": "2020-07-13T07:10:02.676Z",
"contributors": ["Amorymeltzer", "fscholz", "thewub", "PatrickKettner"]
},
"Web/JavaScript/Reference/Errors/Bad_return_or_yield": {
"Web/JavaScript/Reference/Errors/Bad_return": {
"modified": "2020-05-06T09:44:43.817Z",
"contributors": ["fscholz", "PatrickKettner", "nmve", "lewisje"]
},
Expand Down Expand Up @@ -105708,18 +105704,10 @@
"modified": "2020-05-06T09:39:01.985Z",
"contributors": ["fscholz", "PatrickKettner"]
},
"Web/JavaScript/Reference/Errors/Deprecated_String_generics": {
"modified": "2020-04-16T12:48:08.723Z",
"contributors": ["fscholz", "SphinxKnight", "sanjula40"]
},
"Web/JavaScript/Reference/Errors/Deprecated_caller_or_arguments_usage": {
"modified": "2020-04-15T10:56:43.340Z",
"contributors": ["fscholz", "PatrickKettner", "nmve"]
},
"Web/JavaScript/Reference/Errors/Deprecated_expression_closures": {
"modified": "2020-04-16T12:49:19.590Z",
"contributors": ["fscholz", "wbamberg", "SphinxKnight", "sanjula40"]
},
"Web/JavaScript/Reference/Errors/Deprecated_octal": {
"modified": "2020-04-15T11:05:34.536Z",
"contributors": ["fscholz", "PatrickKettner", "non-descriptive"]
Expand All @@ -105728,10 +105716,6 @@
"modified": "2020-11-30T05:50:18.195Z",
"contributors": ["mfuji09", "fscholz"]
},
"Web/JavaScript/Reference/Errors/Deprecated_toLocaleFormat": {
"modified": "2020-04-16T12:42:53.932Z",
"contributors": ["fscholz"]
},
"Web/JavaScript/Reference/Errors/Equal_as_assign": {
"modified": "2020-04-16T08:23:11.906Z",
"contributors": ["fscholz", "arai"]
Expand Down Expand Up @@ -105796,16 +105780,6 @@
"modified": "2020-04-16T12:36:11.639Z",
"contributors": ["fscholz", "PatrickKettner", "thornedlove"]
},
"Web/JavaScript/Reference/Errors/Malformed_formal_parameter": {
"modified": "2020-04-28T13:28:22.712Z",
"contributors": [
"fscholz",
"wbamberg",
"PatrickKettner",
"nmve",
"jorendorff-moz"
]
},
"Web/JavaScript/Reference/Errors/Missing_bracket_after_list": {
"modified": "2020-04-16T07:55:30.556Z",
"contributors": ["fscholz", "nmve"]
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/mozilla/firefox/releases/58/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ _No changes._

### JavaScript

- The non-standard {{jsxref("Date.prototype.toLocaleFormat()")}} method has been removed ([Firefox bug 818634](https://bugzil.la/818634)). See [Warning: Date.prototype.toLocaleFormat is deprecated](/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_toLocaleFormat) for more information and migration help.
- The non-standard [`Date.prototype.toLocaleFormat()`](/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#date_2) method has been removed ([Firefox bug 818634](https://bugzil.la/818634)).
- The non-standard and deprecated {{jsxref("Object.prototype.watch()")}} and {{jsxref("Object.prototype.unwatch", "unwatch()")}} methods have been removed and will no longer work ([Firefox bug 638054](https://bugzil.la/638054)). Consider using [setters and getters](/en-US/docs/Web/JavaScript/Guide/Working_with_objects#defining_getters_and_setters) or [proxies](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) instead.
- The [legacy Iterator protocol](/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#legacy_generator_and_iterator), the [`StopIteration`](/en-US/docs/Archive/Web/StopIteration) object, the [legacy generator functions](/en-US/docs/Web/JavaScript/Reference/Statements/Legacy_generator_function) and the non-standard {{jsxref("Function.prototype.isGenerator()")}} method have been removed. Use the ES2015 [iteration protocols](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) and standards-compliant [iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators) instead ([Firefox bug 1083482](https://bugzil.la/1083482), [Firefox bug 1413867](https://bugzil.la/1413867), [Firefox bug 1119777](https://bugzil.la/1119777)).
- The non-standard [Array comprehensions](/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions) and [Generator comprehensions](/en-US/docs/Web/JavaScript/Reference/Operators/Generator_comprehensions) have been removed ([Firefox bug 1414340](https://bugzil.la/1414340)).
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/mozilla/firefox/releases/68/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ _No changes._
### JavaScript

- The new {{jsxref("BigInt")}} primitive is enabled by default ([Firefox bug 1527902](https://bugzil.la/1527902)).
- String generic methods have been removed ([Firefox bug 1222552](https://bugzil.la/1222552)). See the [deprecation warning](/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_String_generics) for more information.
- [String generic methods](/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#string_2) have been removed ([Firefox bug 1222552](https://bugzil.la/1222552)).

### APIs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ The `valueOf()` method is no longer specialized for `RegExp`. It uses {{jsxref("

### Date

- `Date.prototype.toLocaleFormat()` is obsolete. Use [`toLocaleString`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) or [`Intl.DateTimeFormat`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) instead.
- `Date.prototype.toLocaleFormat()`, which used a format string in the same format expected by the
`strftime()` function in C, is obsolete. Use [`toLocaleString()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) or [`Intl.DateTimeFormat`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) instead.

### Array

Expand Down Expand Up @@ -170,7 +171,7 @@ The `valueOf()` method is no longer specialized for `RegExp`. It uses {{jsxref("

- `for each...in` is obsolete. Use {{jsxref("Statements/for...of", "for...of")}} instead.
- let blocks and let expressions are obsolete.
- [Expression closures](/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_expression_closures) are obsolete. Use regular {{jsxref("Operators/function", "functions")}} or {{jsxref("Functions/Arrow_functions", "arrow functions", "", 1)}} instead.
- Expression closures (`function () 1` as a shorthand of `function () { return 1; }`) are obsolete. Use regular {{jsxref("Operators/function", "functions")}} or {{jsxref("Functions/Arrow_functions", "arrow functions", "", 1)}} instead.

### Acquiring source text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ The argument of {{jsxref("Array.prototype.sort()")}} is expected to be either {{

```js example-bad
[1, 3, 2].sort(5); // TypeError

const cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key] || "asc"); // TypeError
```

### Valid cases

```js example-good
[1, 3, 2].sort(); // [1, 2, 3]

const cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key || "asc"]); // [1, 2, 3]
[1, 3, 2].sort((a, b) => a - b); // [1, 2, 3]
```

## See also
Expand Down
69 changes: 69 additions & 0 deletions files/en-us/web/javascript/reference/errors/bad_await/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: "SyntaxError: await is only valid in async functions, async generators and modules"
slug: Web/JavaScript/Reference/Errors/Bad_await
page-type: javascript-error
---

{{jsSidebar("Errors")}}

The JavaScript exception "await is only valid in async functions, async generators and modules" occurs when an {{jsxref("Operators/await", "await")}} expression is used outside of [async functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) or [modules](/en-US/docs/Web/JavaScript/Guide/Modules) or other async contexts.

## Message

```
SyntaxError: await is only valid in async functions and the top level bodies of modules (V8-based)
SyntaxError: await is only valid in async functions, async generators and modules (Firefox)
SyntaxError: Unexpected identifier (Safari)
```

## Error type

{{jsxref("SyntaxError")}}.

## What went wrong?

JavaScript execution is never blocking: an `await` can never block the execution of the program. Instead, it pauses the execution of the surrounding async task, while allowing other tasks to continue running. Therefore, `await` cannot be used in sync tasks, such as functions, generator functions, or top level of scripts. It is not always apparent whether the current file is a script or a module — see the [Modules guide](/en-US/docs/Web/JavaScript/Guide/Modules#top_level_await) for more information.

## Examples

### Top-level await

You cannot use `await` at the top level of a script:

```html example-bad
<script>
await fetch("https://example.com");
// SyntaxError: await is only valid in async functions, async generators and modules
</script>
```

Instead, make the script a module:

```html example-good
<script type="module">
await fetch("https://example.com");
</script>
```

### Async callbacks

You cannot use `await` in a sync callback:

```js example-bad
urls.forEach((url) => {
await fetch(url);
// SyntaxError: await is only valid in async functions, async generators and modules
});
```

Instead, make the callback async. See more explanation in the [Using promises guide](/en-US/docs/Web/JavaScript/Guide/Using_promises#composition).

```js example-good
Promise.all(urls.map(async (url) => {
await fetch(url);
});
```
## See also
- {{jsxref("Operators/await", "await")}}
134 changes: 134 additions & 0 deletions files/en-us/web/javascript/reference/errors/bad_break/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: "SyntaxError: unlabeled break must be inside loop or switch"
slug: Web/JavaScript/Reference/Errors/Bad_break
page-type: javascript-error
---

{{jsSidebar("Errors")}}

The JavaScript exception "unlabeled break must be inside loop or switch" occurs when a {{jsxref("Statements/break", "break")}} statement is not inside a loop or a {{jsxref("Statements/switch", "switch")}} statement.

## Message

```
SyntaxError: Illegal break statement (V8-based)
SyntaxError: unlabeled break must be inside loop or switch (Firefox)
SyntaxError: 'break' is only valid inside a switch or loop statement. (Safari)
```

## Error type

{{jsxref("SyntaxError")}}.

## What went wrong?

{{jsxref("Statements/break", "break")}} statements can be used to exit a loop or a `switch` statement, and using them elsewhere is a syntax error. Alternatively, you can provide a [label](/en-US/docs/Web/JavaScript/Reference/Statements/label) to the `break` statement to break out of any statement with that label — however, if the label does not reference a containing statement, another error [SyntaxError: label not found](/en-US/docs/Web/JavaScript/Reference/Errors/Label_not_found) will be thrown.

## Examples

### Unsyntactic break

`break` cannot be used outside `switch` or loops.

```js example-bad
let score = 0;

function increment() {
if (score === 100)
break; // SyntaxError: unlabeled break must be inside loop or switch
}
score++;
}
```

Maybe instead of `break`, you intend to use {{jsxref("Statements/return", "return")}} to early-terminate a function.

```js example-good
let score = 0;

function increment() {
if (score === 100)
return;
}
score++;
}
```

### Using break in callbacks

`break` cannot be used in callbacks, even if the callback is called from a loop.

```js example-bad
let containingIndex = 0;
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

while (containingIndex < matrix.length) {
matrix[containingIndex].forEach((value) => {
if (value === 5) {
break; // SyntaxError: unlabeled break must be inside loop or switch
}
});
containingIndex++;
}
```

Instead, refactor the code so the `break` is used outside the callback.

```js example-good
let containingIndex = 0;
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

outer: while (containingIndex < matrix.length) {
for (const value of matrix[containingIndex]) {
if (value === 5) {
break outer;
}
}
containingIndex++;
}
```

```js example-good
let containingIndex = 0;
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

while (containingIndex < matrix.length) {
if (matrix[containingIndex].includes(5))
break;
}
containingIndex++;
}
```

There's no way to early-terminate a {{jsxref("Array/forEach", "forEach()")}} loop. You can use {{jsxref("Array/some", "some()")}} instead, or convert it to a {{jsxref("Statements/for...of", "for...of")}} loop.

```js example-bad
array.forEach((value) => {
if (value === 5) {
break; // SyntaxError: unlabeled break must be inside loop or switch
}
// do something with value
});
```

```js example-good
array.some((value) => {
if (value === 5) {
return true;
}
// do something with value
});
```

```js example-good
for (const value of array) {
if (value === 5) {
break;
}
// do something with value
}
```

## See also

- {{jsxref("Statements/break", "break")}}
Loading

0 comments on commit c495a1e

Please sign in to comment.