forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptException.cpp
More file actions
76 lines (58 loc) · 1.75 KB
/
ScriptException.cpp
File metadata and controls
76 lines (58 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* File: ScriptException.cpp
* Author: ghernan
*
* Exception class to manage script errors.
*
* Created on March 1, 2017, 8:27 PM
*/
#include "ascript_pch.hpp"
#include "ScriptException.h"
#include <string>
using namespace std;
static string generateErrorMessage(const ScriptPosition* pPos, const char* msgFormat, va_list args)
{
char buffer [2048];
string message;
if (pPos)
{
sprintf_s(buffer, "(line: %d, col: %d): ", pPos->line, pPos->column);
message = buffer;
}
vsprintf_s(buffer, msgFormat, args);
message += buffer;
return message;
}
/**
* Generates 'RuntimeError' exception
* @param msgFormat 'printf-like' format string
* @param ... Optional message parameters
*/
void rtError(const char* msgFormat, ...)
{
va_list aptr;
va_start(aptr, msgFormat);
const std::string message = generateErrorMessage(NULL, msgFormat, aptr);
va_end(aptr);
throw RuntimeError(message, VmPosition());
}
/**
* Generates an error message located at the given position
* @param code Pointer to the code location where the error occurs.
* It is used to calculate line and column for the error message
* @param msgFormat 'printf-like' format string
* @param ... Optional message parameters
*/
void errorAt(const ScriptPosition& position, const char* msgFormat, ...)
{
va_list aptr;
va_start(aptr, msgFormat);
const std::string message = generateErrorMessage(&position, msgFormat, aptr);
va_end(aptr);
throw CScriptException(message, position);
}
void errorAt_v(const ScriptPosition& position, const char* msgFormat, va_list args)
{
const std::string message = generateErrorMessage(&position, msgFormat, args);
throw CScriptException(message, position);
}