Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Exceptions.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SCRIPT_EXCEPTIONS
19#define INCLUDED_SCRIPT_EXCEPTIONS
20
21namespace Script { class Request; }
22
23namespace Script
24{
25namespace Exception
26{
27/**
28 * @return Whether there is a JS exception pending.
29 */
30bool IsPending(const Request& rq);
31
32/**
33 * Log and then clear the current pending exception. This function should always be called after calling a
34 * JS script (or anything that can throw JS errors, such as structured clones),
35 * in case that script doesn't catch an exception thrown during its execution.
36 * If no exception is pending, this does nothing.
37 * Note that JS code that wants to throw errors should throw new Error(...), otherwise the stack cannot be used.
38 * @return Whether there was a pending exception.
39 */
40bool CatchPending(const Request& rq);
41
42/**
43 * Raise a JS exception from C++ code.
44 * This is only really relevant in JSNative functions that don't use ObjectOpResult,
45 * as the latter overwrites the pending exception.
46 * Prefer either simply logging an error if you know a stack-trace will be raised elsewhere.
47 */
48void Raise(const Request& rq, const char* format, ...);
49}
50
51}
52#endif // INCLUDED_SCRIPT_EXCEPTIONS
Definition: Request.h:51
void Raise(const Request &rq, const char *format,...)
Raise a JS exception from C++ code.
Definition: Exceptions.cpp:111
bool CatchPending(const Request &rq)
Log and then clear the current pending exception.
Definition: Exceptions.cpp:49
bool IsPending(const Request &rq)
Definition: Exceptions.cpp:44
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32