Skip to content

Commit 2a8a940

Browse files
committed
Merge pull request #1874 from mgreter/bugfix/issue_1807
Improve error handling API (always return formatted json)
2 parents 4d8d1ed + d517067 commit 2a8a940

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/error_handling.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace Sass {
7171
}
7272

7373
DuplicateKeyError::DuplicateKeyError(const Map& dup, const Expression& org)
74-
: dup(dup), org(org)
74+
: Base(org.pstate()), dup(dup), org(org)
7575
{
7676
msg = "Duplicate key ";
7777
dup.get_duplicate_key()->is_delayed(false);
@@ -82,7 +82,7 @@ namespace Sass {
8282
}
8383

8484
TypeMismatch::TypeMismatch(const Expression& var, const std::string type)
85-
: var(var), type(type)
85+
: Base(var.pstate()), var(var), type(type)
8686
{
8787
msg = var.to_string();
8888
msg += " is not an ";

src/error_handling.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,23 @@ namespace Sass {
8686
virtual ~ZeroDivisionError() throw() {};
8787
};
8888

89-
class DuplicateKeyError : public OperationError {
89+
class DuplicateKeyError : public Base {
9090
protected:
9191
const Map& dup;
9292
const Expression& org;
9393
public:
9494
DuplicateKeyError(const Map& dup, const Expression& org);
95-
virtual const char* errtype() const { return "DuplicateKeyError"; }
95+
virtual const char* errtype() const { return "Error"; }
9696
virtual ~DuplicateKeyError() throw() {};
9797
};
9898

99-
class TypeMismatch : public OperationError {
99+
class TypeMismatch : public Base {
100100
protected:
101101
const Expression& var;
102102
const std::string type;
103103
public:
104104
TypeMismatch(const Expression& var, const std::string type);
105-
virtual const char* errtype() const { return "TypeMismatch"; }
105+
virtual const char* errtype() const { return "Error"; }
106106
virtual ~TypeMismatch() throw() {};
107107
};
108108

src/sass_context.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ extern "C" {
123123
msg_stream << "Unable to allocate memory: " << ba.what() << std::endl;
124124
json_append_member(json_err, "status", json_mknumber(2));
125125
json_append_member(json_err, "message", json_mkstring(ba.what()));
126+
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
126127
c_ctx->error_json = json_stringify(json_err, " ");;
127128
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
128129
c_ctx->error_text = sass_strdup(ba.what());
@@ -134,9 +135,10 @@ extern "C" {
134135
catch (std::exception& e) {
135136
std::stringstream msg_stream;
136137
JsonNode* json_err = json_mkobject();
137-
msg_stream << "Error: " << e.what() << std::endl;
138+
msg_stream << "Internal Error: " << e.what() << std::endl;
138139
json_append_member(json_err, "status", json_mknumber(3));
139140
json_append_member(json_err, "message", json_mkstring(e.what()));
141+
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
140142
c_ctx->error_json = json_stringify(json_err, " ");;
141143
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
142144
c_ctx->error_text = sass_strdup(e.what());
@@ -148,9 +150,10 @@ extern "C" {
148150
catch (std::string& e) {
149151
std::stringstream msg_stream;
150152
JsonNode* json_err = json_mkobject();
151-
msg_stream << "Error: " << e << std::endl;
153+
msg_stream << "Internal Error: " << e << std::endl;
152154
json_append_member(json_err, "status", json_mknumber(4));
153155
json_append_member(json_err, "message", json_mkstring(e.c_str()));
156+
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
154157
c_ctx->error_json = json_stringify(json_err, " ");;
155158
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
156159
c_ctx->error_text = sass_strdup(e.c_str());
@@ -162,9 +165,10 @@ extern "C" {
162165
catch (const char* e) {
163166
std::stringstream msg_stream;
164167
JsonNode* json_err = json_mkobject();
165-
msg_stream << "Error: " << e << std::endl;
168+
msg_stream << "Internal Error: " << e << std::endl;
166169
json_append_member(json_err, "status", json_mknumber(4));
167170
json_append_member(json_err, "message", json_mkstring(e));
171+
json_append_member(json_err, "formatted", json_mkstring(msg_stream.str().c_str()));
168172
c_ctx->error_json = json_stringify(json_err, " ");;
169173
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
170174
c_ctx->error_text = sass_strdup(e);

0 commit comments

Comments
 (0)