An error from a JavaScript operation.
The library never throws C++ exceptions. Operations that can fail return a js::Result, and its Error is one of four kinds:
- A JavaScript exception (is_exception() is true): script threw, or the call was invalid in JavaScript (eg, calling something that isn't a function). exception() holds the thrown value, and type(), stack(), source_url(), line(), and column() describe it.
- Empty handle (is_empty() is true): the Value or Context you called it on holds nothing (see js::Value). This usually means a bug in your code.
- Page gone (is_page_gone() is true): the handle's page is gone (see js::Value).
- A native error (all three are false): an error created in C++, by you (TypeError(), RangeError(), Make()) or by the library (eg, when To() gets the wrong type).
A page can go away at any time, so a page-gone error usually needs no report. Report the rest with message():
if (!title) {
if (title.error().is_page_gone())
return;
Log(title.error().message());
}
Expected< T, Error > Result
The result of a JavaScript operation that can fail: a T or a js::Error.
Definition Error.h:520
A bound function throws an Error into the page by returning it in a js::Result. Use WithCode() to give it a code property, so page script can check the kind of error without parsing the message.
Error Codes
The TypeErrors the library creates have a code property (read it with code() in native code, or error.code in page script). Check the code rather than the message, since message text is written for people and can change in any release. The codes below stay the same (new ones may be added).
| Code | Meaning |
| ULJS_BAD_ARG | A value doesn't have the expected type, or its text doesn't parse |
| ULJS_LOSSY | A number doesn't fit the parameter's type (Strict diagnostics only) |
| ULJS_EXTRA_ARGS | A call passed more arguments than the binding takes (Strict only) |
| ULJS_DETACHED | The native object or API behind a binding or instance is gone |
| ULJS_NOT_INSTANCE | A class method was called on an object that isn't an instance |
| ULJS_NO_CTOR | Page script used new on a class that has no constructor |
| ULJS_CROSS_CONTEXT | A value was used on a page it can't be shared with (see js::Value) |
- Note
- When JavaScript diagnostics are on (see js::Diagnostics; they're on by default in developer mode), an Error that holds a JavaScript exception logs it if it's destroyed before anything reads it (any accessor, such as message(), counts). So a dropped Result never hides a script error during development.
|
| | Error (Error &&other) noexcept |
| | Move constructor (other no longer holds the exception).
|
| Error & | operator= (Error &&other) noexcept |
| | Move assignment (other no longer holds the exception).
|
| | Error (const Error &other) |
| | Copy constructor (takes its own reference to the exception, if any).
|
| Error & | operator= (const Error &other) |
| | Copy assignment (takes its own reference to the exception, if any).
|
| | ~Error () |
| | Destructor (logs the exception first if nothing read it; see the class notes).
|
| Error | WithCode (std::string code) && |
| | Add a code that page script gets as the error's code property when it's thrown.
|
| bool | is_exception () const |
| | Whether or not this error holds a JavaScript exception.
|
| bool | is_page_gone () const |
| | Whether or not this error means the handle's page is gone.
|
| bool | is_empty () const |
| | Whether or not this error means the handle holds nothing (see the class notes).
|
| ULJSValue | exception () const |
| | Get the JavaScript exception (NULL if is_exception() is false).
|
| ErrorType | type () const |
| | Get the error's type.
|
| std::string | stack () const |
| | Get the stack trace of a JavaScript exception.
|
| std::string | source_url () const |
| | Get the URL of the script that created a JavaScript exception.
|
| unsigned | line () const |
| | Get the 1-based line where a JavaScript exception was created.
|
| unsigned | column () const |
| | Get the 1-based column where a JavaScript exception was created.
|
| std::string | code () const |
| | Get the error's code (an empty string if it has none).
|
| ULJSValue | ToJS (ULJSContext ctx) const |
| | Convert this error to a JavaScript value.
|
| std::string | message () const |
| | Get a readable description of the error.
|