Skip to content

Commit 1f8c5a0

Browse files
committed
Refactor and unify IncompatibleUnits exception
1 parent 0444f85 commit 1f8c5a0

File tree

5 files changed

+17
-26
lines changed

5 files changed

+17
-26
lines changed

src/ast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ namespace Sass {
18671867
// maybe convert to other unit
18681868
// easier implemented on its own
18691869
try { convert(prefered, strict); }
1870-
catch (incompatibleUnits& err)
1870+
catch (Exception::IncompatibleUnits& err)
18711871
{ error(err.what(), pstate()); }
18721872
catch (...) { throw; }
18731873

src/error_handling.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ namespace Sass {
111111
}
112112

113113
IncompatibleUnits::IncompatibleUnits(const Number& lhs, const Number& rhs)
114-
: lhs(lhs), rhs(rhs)
115114
{
116115
msg = "Incompatible units: '";
117116
msg += rhs.unit();
@@ -120,6 +119,15 @@ namespace Sass {
120119
msg += "'.";
121120
}
122121

122+
IncompatibleUnits::IncompatibleUnits(const UnitType lhs, const UnitType rhs)
123+
{
124+
msg = "Incompatible units: '";
125+
msg += unit_to_string(rhs);
126+
msg += "' and '";
127+
msg += unit_to_string(lhs);
128+
msg += "'.";
129+
}
130+
123131
AlphaChannelsNotEqual::AlphaChannelsNotEqual(Expression_Ptr_Const lhs, Expression_Ptr_Const rhs, const std::string& op)
124132
: lhs(lhs), rhs(rhs), op(op)
125133
{

src/error_handling.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <sstream>
66
#include <stdexcept>
77
#include "position.hpp"
8+
#include "ast_fwd_decl.hpp"
9+
#include "sass/functions.h"
810

911
namespace Sass {
1012

@@ -136,10 +138,11 @@ namespace Sass {
136138

137139
class IncompatibleUnits : public OperationError {
138140
protected:
139-
const Number& lhs;
140-
const Number& rhs;
141+
// const Sass::UnitType lhs;
142+
// const Sass::UnitType rhs;
141143
public:
142144
IncompatibleUnits(const Number& lhs, const Number& rhs);
145+
IncompatibleUnits(const UnitType lhs, const UnitType rhs);
143146
virtual ~IncompatibleUnits() throw() {};
144147
};
145148

src/units.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "sass.hpp"
22
#include <stdexcept>
33
#include "units.hpp"
4+
#include "error_handling.hpp"
45

56
namespace Sass {
67

@@ -177,7 +178,7 @@ namespace Sass {
177178
size_t i2 = u2 - t2;
178179
// error if units are not of the same group
179180
// don't error for multiplication and division
180-
if (strict && t1 != t2) throw incompatibleUnits(u1, u2);
181+
if (strict && t1 != t2) throw Exception::IncompatibleUnits(u1, u2);
181182
// only process known units
182183
if (u1 != UNKNOWN && u2 != UNKNOWN) {
183184
switch (t1) {

src/units.hpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,6 @@ namespace Sass {
6666
// throws incompatibleUnits exceptions
6767
double conversion_factor(const std::string&, const std::string&, bool = true);
6868

69-
class incompatibleUnits: public std::exception
70-
{
71-
public:
72-
const char* msg;
73-
incompatibleUnits(Sass::UnitType a, Sass::UnitType b)
74-
: exception()
75-
{
76-
std::stringstream ss;
77-
ss << "Incompatible units: ";
78-
ss << "'" << unit_to_string(a) << "' and ";
79-
ss << "'" << unit_to_string(b) << "'";
80-
// hold on to string on stack!
81-
std::string str(ss.str());
82-
msg = str.c_str();
83-
}
84-
virtual const char* what() const throw()
85-
{
86-
return msg;
87-
}
88-
};
89-
9069
}
9170

9271
#endif

0 commit comments

Comments
 (0)