Qore Programming Language 1.19.5
Loading...
Searching...
No Matches
ExceptionSink.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 ExceptionSink.h
4
5 Qore Programming Language ExceptionSink class definition
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_EXCEPTIONSINK_H
33
34#define _QORE_EXCEPTIONSINK_H
35
36#include <cstdarg>
37#include <cstdio>
38#include <string>
39#include <vector>
40
41// forward references
42class QoreException;
43class ExceptionSink;
45hashdecl QoreProgramLocation;
46hashdecl QoreCallStack;
47class AbstractStatement;
48
51 friend hashdecl qore_es_private;
52 friend QoreXSinkException;
53
54public:
56 DLLEXPORT ExceptionSink();
57
59 DLLEXPORT ~ExceptionSink();
60
62 DLLEXPORT void handleExceptions();
63
65 DLLEXPORT void handleWarnings();
66
68 DLLEXPORT bool isEvent() const;
69
71 DLLEXPORT bool isThreadExit() const;
72
74 DLLEXPORT bool isException() const;
75
77
83 DLLEXPORT operator bool () const;
84
86
91 DLLEXPORT AbstractQoreNode* raiseException(const char *err, const char *fmt, ...);
92
94
100 DLLEXPORT AbstractQoreNode* raiseErrnoException(const char *err, int en, const char *fmt, ...);
101
103
109 DLLEXPORT AbstractQoreNode* raiseErrnoException(const char *err, int en, QoreStringNode* desc);
110
112
118 DLLEXPORT AbstractQoreNode* raiseExceptionArg(const char* err, QoreValue arg, const char* fmt, ...);
119
121
127 DLLEXPORT AbstractQoreNode* raiseExceptionArg(const char* err, QoreValue arg, QoreStringNode* desc);
128
130
140 DLLEXPORT AbstractQoreNode* raiseExceptionArg(const char* err, QoreValue arg, QoreStringNode* desc, const QoreCallStack& stack);
141
143
154 DLLEXPORT AbstractQoreNode* raiseExceptionArg(const QoreProgramLocation& loc, const char* err, QoreValue arg, QoreStringNode* desc, const QoreCallStack& stack);
155
157
167 DLLEXPORT AbstractQoreNode* raiseExceptionArg(const QoreProgramLocation& loc, const char* err, QoreValue arg, QoreStringNode* desc);
168
170
175 DLLEXPORT AbstractQoreNode* raiseException(const char *err, QoreStringNode* desc);
176
178
184
186
197
199
207 DLLEXPORT void raiseException(const QoreProgramLocation& loc, const char* err, QoreValue arg, QoreValue desc);
208
210
218 DLLEXPORT void raiseException(const QoreProgramLocation& loc, const char* err, QoreValue arg, const char* fmt, ...);
219
221 DLLEXPORT void raiseThreadExit();
222
224 DLLEXPORT void assimilate(ExceptionSink *xs);
225
227 DLLEXPORT void assimilate(ExceptionSink &xs);
228
230
232 DLLEXPORT void outOfMemory();
233
235 DLLEXPORT void clear();
236
238 DLLEXPORT const QoreValue getExceptionErr();
239
241 DLLEXPORT const QoreValue getExceptionDesc();
242
244 DLLEXPORT const QoreValue getExceptionArg();
245
247
251 DLLEXPORT int appendLastDescription(const char* fmt, ...);
252
253 // Renames the "err" key of the top exception and prepends a string description to any desc value
258 DLLEXPORT int renamePrependLastException(const char* err, const char* desc_fmt, ...);
259
261
264
265 DLLLOCAL void raiseException(QoreException* e);
266 DLLLOCAL void raiseException(const QoreListNode* n);
267 DLLLOCAL QoreException* catchException();
268 DLLLOCAL QoreException* getException();
269 DLLLOCAL void overrideLocation(const QoreProgramLocation& loc);
270 DLLLOCAL void rethrow(QoreException* old);
271
272 DLLLOCAL static void defaultExceptionHandler(QoreException* e);
273 DLLLOCAL static void defaultWarningHandler(QoreException* e);
274
275 DLLLOCAL static void outputExceptionLocation(const char* fns, int start_line, int end_line, const char* srcs,
276 int offset, const char* langs, const char* types);
277
278private:
280 hashdecl qore_es_private* priv;
281
282 ExceptionSink(const ExceptionSink&) = delete;
283 ExceptionSink& operator=(const ExceptionSink&) = delete;
284};
285
287enum qore_call_t : signed char {
288 CT_UNUSED = -1,
289 CT_USER = 0,
290 CT_BUILTIN = 1,
291 CT_NEWTHREAD = 2,
292 CT_RETHROW = 3
293};
294
296
299 std::string label;
302 std::string source;
303 unsigned offset = 0;
304 std::string code;
305 std::string lang;
306
307 DLLLOCAL QoreSourceLocation(const char* label, int start, int end, const char* code, const char* lang = "Qore") :
308 label(label), start_line(start), end_line(end), code(code), lang(lang) {
309 }
310
311 DLLLOCAL QoreSourceLocation(const char* label, int start, int end, const char* source, unsigned offset,
312 const char* code, const char* lang = "Qore") :
314 }
315};
316
318
321 qore_call_t type;
322
323 DLLLOCAL QoreCallStackElement(qore_call_t type, const char* label, int start, int end, const char* code,
324 const char* lang = "Qore") :
325 QoreSourceLocation(label, start, end, code, lang), type(type) {
326 }
327
328 DLLLOCAL QoreCallStackElement(qore_call_t type, const char* label, int start, int end, const char* source,
329 unsigned offset, const char* code, const char* lang = "Qore") :
331 }
332};
333
334typedef std::vector<QoreCallStackElement> callstack_vec_t;
335
337
339hashdecl QoreCallStack : public callstack_vec_t {
341 DLLEXPORT void add(qore_call_t type, const char* label, int start, int end, const char* code,
342 const char* lang = "Qore");
343
345 DLLEXPORT void add(qore_call_t type, const char* label, int start, int end, const char* source,
346 unsigned offset, const char* code, const char* lang = "Qore");
347};
348
349static inline void alreadyDeleted(ExceptionSink *xsink, const char *cmeth) {
350 xsink->raiseException("OBJECT-ALREADY-DELETED", "the method %s() cannot be executed because the object has already been deleted", cmeth);
351}
352
353static inline void makeAccessDeletedObjectException(ExceptionSink *xsink, const char *mem, const char *cname) {
354 xsink->raiseException("OBJECT-ALREADY-DELETED", "attempt to access member '%s' of an already-deleted object of class '%s'", mem, cname);
355}
356
357static inline void makeAccessDeletedObjectException(ExceptionSink *xsink, const char *cname) {
358 xsink->raiseException("OBJECT-ALREADY-DELETED", "attempt to access an already-deleted object of class '%s'", cname);
359}
360
363public:
366
369
372
374 DLLEXPORT QoreExternalProgramLocationWrapper(const char* file, int start_line, int end_line,
375 const char* source = nullptr, int offset = 0, const char* lang = nullptr);
376
379
381 DLLEXPORT void set(const char* file, int start_line, int end_line,
382 const char* source = nullptr, int offset = 0, const char* lang = nullptr);
383
385 DLLLOCAL const QoreProgramLocation& get() const {
386 return *loc;
387 }
388
390 DLLLOCAL const std::string& getFile() const {
391 return file_str;
392 }
393
395 DLLLOCAL const std::string& getSource() const {
396 return source_str;
397 }
398
400 DLLLOCAL const std::string& getLanguage() const {
401 return lang_str;
402 }
403
405 DLLEXPORT int getStartLine() const;
406
408 DLLEXPORT int getEndLine() const;
409
410private:
411 // save strings for exceptions in case they are epheremal when this object is created
412 std::string file_str;
413 std::string source_str;
414 std::string lang_str;
415
416 // actual exception location
417 QoreProgramLocation* loc;
418};
419
421
424public:
427
429 DLLLOCAL QoreStackLocation(const QoreStackLocation&) = default;
430
433
435 DLLLOCAL virtual ~QoreStackLocation() = default;
436
438 DLLLOCAL QoreStackLocation& operator=(const QoreStackLocation&) = default;
439
442
444
451 DLLLOCAL void setNext(const QoreStackLocation* next) {
452 stack_next = next;
453 }
454
456 DLLLOCAL virtual const QoreStackLocation* getNext() const {
457 return stack_next;
458 }
459
461 DLLLOCAL virtual QoreProgram* getProgram() const = 0;
462
464 DLLLOCAL virtual const AbstractStatement* getStatement() const = 0;
465
467 DLLLOCAL virtual const std::string& getCallName() const = 0;
468
470 DLLLOCAL virtual qore_call_t getCallType() const = 0;
471
473 DLLLOCAL virtual const QoreProgramLocation& getLocation() const = 0;
474
475protected:
476 const QoreStackLocation* stack_next = nullptr;
477};
478
480
483 friend class qore_external_runtime_stack_location_helper_priv;
484public:
487
490
493
495 DLLEXPORT virtual ~QoreExternalStackLocation();
496
499
502
504 DLLEXPORT virtual QoreProgram* getProgram() const;
505
507 DLLEXPORT virtual const AbstractStatement* getStatement() const;
508
509private:
510 class qore_external_stack_location_priv* priv;
511};
512
514
517public:
520
523
526
529
532
535
536private:
537 class qore_external_runtime_stack_location_helper_priv* priv;
538};
539
540#endif
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
DLLEXPORT void assimilate(ExceptionSink *xs)
assimilates all entries of the "xs" argument by appending them to the internal list and deletes the "...
DLLEXPORT bool isEvent() const
returns true if at least one exception is present or thread_exit has been triggered
DLLEXPORT const QoreValue getExceptionErr()
returns the error of the top exception
DLLEXPORT void assimilate(ExceptionSink &xs)
assimilates all entries of the "xs" argument by appending them to the internal list and clears the "x...
DLLEXPORT void raiseThreadExit()
sets the "thread_exit" flag; will cause the current thread to terminate
DLLEXPORT const QoreValue getExceptionArg()
returns the argument of the top exception
DLLEXPORT AbstractQoreNode * raiseException(QoreStringNode *err, QoreStringNode *desc)
appends a Qore-language exception to the list; takes owenership of the "desc" argument reference
DLLEXPORT const QoreValue getExceptionDesc()
returns the description of the top exception
DLLEXPORT int appendLastDescription(const char *fmt,...)
appends a formatted string to the top exception description if the desc value is a string
DLLEXPORT AbstractQoreNode * raiseException(QoreStringNode *err, QoreStringNode *desc, QoreValue arg)
appends a Qore-language exception to the list; takes owenership of the "desc" argument reference
DLLEXPORT AbstractQoreNode * raiseExceptionArg(const QoreProgramLocation &loc, const char *err, QoreValue arg, QoreStringNode *desc, const QoreCallStack &stack)
appends a Qore-language exception to the list, and sets the 'arg' member (this object takes over the ...
DLLEXPORT void outOfMemory()
intended to be used to handle out of memory errors
DLLEXPORT int renamePrependLastException(const char *err, const char *desc_fmt,...)
DLLEXPORT void handleWarnings()
calls ExceptionSink::defaultWarningHandler() on all exceptions still present in the object and then d...
DLLEXPORT void raiseException(const QoreProgramLocation &loc, const char *err, QoreValue arg, QoreValue desc)
appends a Qore-language exception to the list; takes owenership of the "desc" argument reference
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
DLLEXPORT AbstractQoreNode * raiseExceptionArg(const char *err, QoreValue arg, QoreStringNode *desc, const QoreCallStack &stack)
appends a Qore-language exception to the list, and sets the 'arg' member (this object takes over the ...
DLLEXPORT void handleExceptions()
calls ExceptionSink::defaultExceptionHandler() on all exceptions still present in the object and then...
DLLEXPORT AbstractQoreNode * raiseErrnoException(const char *err, int en, const char *fmt,...)
appends a Qore-language exception to the list and appends the result of strerror(errno) to the descri...
DLLEXPORT void clear()
deletes the exception list immediately
DLLEXPORT AbstractQoreNode * raiseExceptionArg(const char *err, QoreValue arg, const char *fmt,...)
appends a Qore-language exception to the list, and sets the 'arg' member (this object takes over the ...
DLLEXPORT void raiseException(const QoreProgramLocation &loc, const char *err, QoreValue arg, const char *fmt,...)
appends a Qore-language exception to the list; takes owenership of the "desc" argument reference
DLLEXPORT QoreHashNode * getExceptionInfo()
Consumes the exception and returns an ExceptionInfo hash for it.
DLLEXPORT AbstractQoreNode * raiseException(const char *err, QoreStringNode *desc)
appends a Qore-language exception to the list; takes owenership of the "desc" argument reference
DLLEXPORT ~ExceptionSink()
calls ExceptionSink::defaultExceptionHandler() on all exceptions still present in the object and then...
DLLEXPORT AbstractQoreNode * raiseExceptionArg(const char *err, QoreValue arg, QoreStringNode *desc)
appends a Qore-language exception to the list, and sets the 'arg' member (this object takes over the ...
DLLEXPORT ExceptionSink()
creates an empty ExceptionSink object
DLLEXPORT bool isThreadExit() const
returns true if thread_exit has been triggered
DLLEXPORT AbstractQoreNode * raiseErrnoException(const char *err, int en, QoreStringNode *desc)
appends a Qore-language exception to the list and appends the result of strerror(errno) to the descri...
DLLEXPORT AbstractQoreNode * raiseExceptionArg(const QoreProgramLocation &loc, const char *err, QoreValue arg, QoreStringNode *desc)
appends a Qore-language exception to the list, and sets the 'arg' member (this object takes over the ...
DLLEXPORT bool isException() const
returns true if at least one exception is present
returns a custom Qore program location for external modules to generate runtime exceptions with the s...
Definition: ExceptionSink.h:362
DLLEXPORT QoreExternalProgramLocationWrapper(QoreExternalProgramLocationWrapper &&)
move ctor
DLLLOCAL const QoreProgramLocation & get() const
returns the source location
Definition: ExceptionSink.h:385
DLLEXPORT int getEndLine() const
returns the start line
DLLEXPORT QoreExternalProgramLocationWrapper(const QoreExternalProgramLocationWrapper &)
copy ctor
DLLEXPORT QoreExternalProgramLocationWrapper()
empty constructor; use set() to set the location
DLLEXPORT QoreExternalProgramLocationWrapper(const char *file, int start_line, int end_line, const char *source=nullptr, int offset=0, const char *lang=nullptr)
constructor setting the source location
DLLEXPORT void set(const char *file, int start_line, int end_line, const char *source=nullptr, int offset=0, const char *lang=nullptr)
sets the program source location
DLLLOCAL const std::string & getLanguage() const
returns the language
Definition: ExceptionSink.h:400
DLLLOCAL const std::string & getFile() const
returns the file name
Definition: ExceptionSink.h:390
DLLLOCAL const std::string & getSource() const
returns the source
Definition: ExceptionSink.h:395
DLLEXPORT ~QoreExternalProgramLocationWrapper()
destructor; frees memory
DLLEXPORT int getStartLine() const
returns the start line
Sets the stack location for external modules providing language support.
Definition: ExceptionSink.h:516
DLLEXPORT QoreExternalRuntimeStackLocationHelper(const QoreExternalRuntimeStackLocationHelper &)
copy ctor
DLLLOCAL QoreExternalRuntimeStackLocationHelper & operator=(const QoreExternalRuntimeStackLocationHelper &)=delete
no assignment operator
DLLEXPORT QoreExternalRuntimeStackLocationHelper()
Sets the current runtime location.
DLLLOCAL QoreExternalRuntimeStackLocationHelper & operator=(QoreExternalRuntimeStackLocationHelper &&)=delete
no move assignment operator
DLLEXPORT ~QoreExternalRuntimeStackLocationHelper()
Restores the old runtime location.
DLLEXPORT QoreExternalRuntimeStackLocationHelper(QoreExternalRuntimeStackLocationHelper &&)
move ctor
Stack location element abstract class for external binary modules.
Definition: ExceptionSink.h:482
DLLLOCAL QoreExternalStackLocation & operator=(const QoreExternalStackLocation &)=delete
no assignment operator
virtual DLLEXPORT ~QoreExternalStackLocation()
destroys the object
virtual DLLEXPORT QoreProgram * getProgram() const
returns the QoreProgram container
DLLEXPORT QoreExternalStackLocation()
create the object
virtual DLLEXPORT const AbstractStatement * getStatement() const
returns the statement for the call for internal Qore code
DLLEXPORT QoreExternalStackLocation(QoreExternalStackLocation &&)
move ctor
DLLEXPORT QoreExternalStackLocation(const QoreExternalStackLocation &)
copy ctor
DLLLOCAL QoreExternalStackLocation & operator=(QoreExternalStackLocation &&)=delete
no move assignment operator
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only
Definition: QoreProgram.h:128
Stack location element abstract class.
Definition: ExceptionSink.h:423
DLLLOCAL QoreStackLocation & operator=(QoreStackLocation &&)=default
default move assignment operator
virtual DLLLOCAL const QoreProgramLocation & getLocation() const =0
returns the source location of the element
DLLLOCAL QoreStackLocation & operator=(const QoreStackLocation &)=default
default assignment operator
virtual DLLLOCAL const std::string & getCallName() const =0
returns the name of the function or method call
virtual DLLLOCAL ~QoreStackLocation()=default
virtual destructor
virtual DLLLOCAL QoreProgram * getProgram() const =0
returns the QoreProgram container
DLLLOCAL void setNext(const QoreStackLocation *next)
called when pushed on the stack to set the next location
Definition: ExceptionSink.h:451
virtual DLLLOCAL const QoreStackLocation * getNext() const
returns the next location in the stack or nullptr if there is none
Definition: ExceptionSink.h:456
virtual DLLLOCAL const AbstractStatement * getStatement() const =0
returns the statement for the call for internal Qore code
DLLLOCAL QoreStackLocation(const QoreStackLocation &)=default
copy ctor
DLLLOCAL QoreStackLocation(QoreStackLocation &&)=default
move ctor
DLLLOCAL QoreStackLocation()
constructor
virtual DLLLOCAL qore_call_t getCallType() const =0
returns the call type
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
class for C++ exception based on an ExceptionSink object
Definition: QoreXSinkException.h:51
call stack element; strings must be in the default encoding for the Qore process
Definition: ExceptionSink.h:320
qore_call_t type
the call stack element type
Definition: ExceptionSink.h:321
Qore call stack.
Definition: ExceptionSink.h:339
DLLEXPORT void add(qore_call_t type, const char *label, int start, int end, const char *source, unsigned offset, const char *code, const char *lang="Qore")
add an element to the end of the stack trace
DLLEXPORT void add(qore_call_t type, const char *label, int start, int end, const char *code, const char *lang="Qore")
add an element to the end of the stack trace
Qore source location; strings must be in the default encoding for the Qore process.
Definition: ExceptionSink.h:298
unsigned offset
offset in source file (only used if source is not empty)
Definition: ExceptionSink.h:303
std::string label
the code label name (source file if source not present)
Definition: ExceptionSink.h:299
int end_line
the end line
Definition: ExceptionSink.h:301
int start_line
the start line
Definition: ExceptionSink.h:300
std::string code
the function or method call name; method calls in format class::name
Definition: ExceptionSink.h:304
std::string source
optional additional source file
Definition: ExceptionSink.h:302
std::string lang
the source language
Definition: ExceptionSink.h:305
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:279