Overte C++ Documentation
ScriptException.h
1 //
2 // ScriptException.h
3 // libraries/script-engine/src
4 //
5 // Created by Dale Glass on 27/02/2023.
6 // Copyright 2023 Overte e.V.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 
11 #include <QObject>
12 #include <QString>
13 #include <QStringList>
14 
15 #include "ScriptValue.h"
16 
17 #pragma once
18 
27  public:
28 
29  ScriptException(QString message = "", QString info = "", int line = 0, int column = 0, QStringList backtraceList = QStringList()) :
30  errorMessage(message), additionalInfo(info), errorLine(line), errorColumn(column), backtrace(backtraceList) {
31 
32  }
33 
38  QString errorMessage;
39 
47  QString additionalInfo;
48 
49 
54  int errorLine;
55 
61 
66  QStringList backtrace;
67 
68  bool isEmpty() const { return errorMessage.isEmpty(); }
69 
79  virtual std::shared_ptr<ScriptException> clone() const {
80  return std::make_shared<ScriptException>(*this);
81  }
82 };
83 
84 
93  public:
94 
95  ScriptEngineException(QString message = "", QString info = "", int line = 0, int column = 0, QStringList backtraceList = QStringList()) :
96  ScriptException(message, info, line, column, backtraceList) {
97 
98  }
99 
100 
110  virtual std::shared_ptr<ScriptException> clone() const override {
111  return std::make_shared<ScriptEngineException>(*this);
112  }
113 };
114 
115 
116 
124  public:
125 
126  ScriptRuntimeException(QString message = "", QString info = "", int line = 0, int column = 0, QStringList backtraceList = QStringList()) :
127  ScriptException(message, info, line, column, backtraceList) {
128 
129  }
130 
131 
139 
149  virtual std::shared_ptr<ScriptException> clone() const override {
150  return std::make_shared<ScriptRuntimeException>(*this);
151  }
152 };
153 
154 inline QDebug operator<<(QDebug debug, const ScriptException& e) {
155  debug << "Exception:"
156  << e.errorMessage
157  << (e.additionalInfo.isEmpty() ? QString("") : "[" + e.additionalInfo + "]")
158  << " at line " << e.errorLine << ", column " << e.errorColumn;
159 
160  if (e.backtrace.length()) {
161  debug << "Backtrace:";
162  debug << e.backtrace;
163  }
164 
165  return debug;
166 }
167 
168 // Is this a bad practice?
169 inline QDebug operator<<(QDebug debug, std::shared_ptr<ScriptException> e) {
170  if (!e) {
171  debug << "[Null ScriptException]";
172  return debug;
173  }
174 
175  debug << *e.get();
176  return debug;
177 }
178 
179 Q_DECLARE_METATYPE(ScriptException)
180 Q_DECLARE_METATYPE(ScriptEngineException)
181 Q_DECLARE_METATYPE(ScriptRuntimeException)
182 
183 Q_DECLARE_METATYPE(std::shared_ptr<ScriptException>)
184 Q_DECLARE_METATYPE(std::shared_ptr<ScriptEngineException>)
185 Q_DECLARE_METATYPE(std::shared_ptr<ScriptRuntimeException>)
186 
Exception that ocurred inside the scripting engine on the c++ side.
Definition: ScriptException.h:92
virtual std::shared_ptr< ScriptException > clone() const override
Clones this object.
Definition: ScriptException.h:110
Scripting exception.
Definition: ScriptException.h:26
int errorColumn
Error column.
Definition: ScriptException.h:60
int errorLine
Error line.
Definition: ScriptException.h:54
QString errorMessage
Error message.
Definition: ScriptException.h:38
QStringList backtrace
Backtrace.
Definition: ScriptException.h:66
QString additionalInfo
Additional information about the exception.
Definition: ScriptException.h:47
virtual std::shared_ptr< ScriptException > clone() const
Clones this object.
Definition: ScriptException.h:79
Exception that ocurred inside the running script.
Definition: ScriptException.h:123
ScriptValue thrownValue
The actual value that was thrown by the script.
Definition: ScriptException.h:138
virtual std::shared_ptr< ScriptException > clone() const override
Clones this object.
Definition: ScriptException.h:149
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40