Skip to content

Commit

Permalink
Rework all exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 19, 2024
1 parent 8b50283 commit 00b4f65
Show file tree
Hide file tree
Showing 55 changed files with 2,583 additions and 1,154 deletions.
3 changes: 3 additions & 0 deletions include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include <string>
#include <utility>

#include <fmt/core.h>

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"

namespace fintamath {

Expand Down
8 changes: 7 additions & 1 deletion include/fintamath/core/IArithmeticCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ class IArithmeticCRTP_ : public IArithmetic {
return cast<IArithmetic>(res->toMinimalObject());
}

throw InvalidInputBinaryOperatorException(operStr, toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of "{}" operator ({} "{}" and {} "{}" are unconvertible to each other))",
operStr,
this->getType().getName(),
this->toString(),
rhs.getType().getName(),
rhs.toString()));
}

private:
Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <utility>

#include <fmt/core.h>

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/MathObjectType.hpp"
Expand Down
7 changes: 6 additions & 1 deletion include/fintamath/core/IComparableCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class IComparableCRTP_ : public IComparable {
return 0 <=> (rhs <=> *this);
}

throw InvalidInputBinaryOperatorException("<=>", toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of comparison operator ({} "{}" and {} "{}" are unconvertible to each other))",
this->getType().getName(),
this->toString(),
rhs.getType().getName(),
rhs.toString()));
}

private:
Expand Down
10 changes: 9 additions & 1 deletion include/fintamath/exceptions/Exception.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#pragma once

#include <exception>
#include <string>
#include <string_view>

namespace fintamath {

class Exception : public std::exception {
public:
explicit Exception(const std::string_view inMessage) : message(inMessage) {
}

const char *what() const noexcept override {
return "Something went wrong...";
return message.data();
}

private:
std::string message;
};

}
47 changes: 8 additions & 39 deletions include/fintamath/exceptions/InvalidInputException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,29 @@

#include <cstdint>
#include <string>
#include <vector>
#include <string_view>

#include "fintamath/exceptions/Exception.hpp"

namespace fintamath {

class InvalidInputException : public Exception {
public:
InvalidInputException() = default;

explicit InvalidInputException(const std::string &input) {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
explicit InvalidInputException(const std::string_view inMessage) : Exception(inMessage) {
}

protected:
std::string content = "Invalid input";
};

class InvalidInputFunctionException final : public InvalidInputException {
public:
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &argVect) {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &)
: InvalidInputException(func) {
}
};

class InvalidInputBinaryOperatorException final : public InvalidInputException {
public:
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &, const std::string &)
: InvalidInputException(oper) {
}
};

Expand All @@ -60,15 +36,8 @@ class InvalidInputUnaryOperatorException final : public InvalidInputException {
};

public:
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &, const Type)
: InvalidInputException(oper) {
}
};

Expand Down
47 changes: 8 additions & 39 deletions include/fintamath/exceptions/UndefinedException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,29 @@

#include <cstdint>
#include <string>
#include <vector>
#include <string_view>

#include "fintamath/exceptions/Exception.hpp"

namespace fintamath {

class UndefinedException : public Exception {
public:
UndefinedException() = default;

explicit UndefinedException(const std::string &input) {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
explicit UndefinedException(const std::string_view inMessage) : Exception(inMessage) {
}

protected:
std::string content = "Undefined";
};

class UndefinedFunctionException final : public UndefinedException {
public:
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &argVect) {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &)
: UndefinedException(func) {
}
};

class UndefinedBinaryOperatorException final : public UndefinedException {
public:
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &, const std::string &)
: UndefinedException(oper) {
}
};

Expand All @@ -60,15 +36,8 @@ class UndefinedUnaryOperatorException final : public UndefinedException {
};

public:
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &, const Type)
: UndefinedException(oper) {
}
};

Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <utility>
#include <vector>

#include <fmt/core.h>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.hpp"
Expand Down
18 changes: 9 additions & 9 deletions include/fintamath/expressions/IExpressionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP<Derived> {
}

protected:
Derived &add(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("+", this->toString(), rhs.toString());
Derived &add(const Derived &) override {
throw InvalidInputException(fmt::format("Arithmetic operations are not permitted for {}", this->getType().getName()));
}

Derived &substract(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("-", this->toString(), rhs.toString());
Derived &substract(const Derived &) override {
throw InvalidInputException(fmt::format("Arithmetic operations are not permitted for {}", this->getType().getName()));
}

Derived &multiply(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("*", this->toString(), rhs.toString());
Derived &multiply(const Derived &) override {
throw InvalidInputException(fmt::format("Arithmetic operations are not permitted for {}", this->getType().getName()));
}

Derived &divide(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("/", this->toString(), rhs.toString());
Derived &divide(const Derived &) override {
throw InvalidInputException(fmt::format("Arithmetic operations are not permitted for {}", this->getType().getName()));
}

Derived &negate() override {
throw InvalidInputUnaryOperatorException("-", this->toString(), InvalidInputUnaryOperatorException::Type::Prefix);
throw InvalidInputException(fmt::format("Arithmetic operations are not permitted for {}", this->getType().getName()));
}

private:
Expand Down
6 changes: 3 additions & 3 deletions include/fintamath/literals/Boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Boolean final : public ILiteralCRTP<Boolean> {
public:
Boolean();

explicit Boolean(const std::string &str);
explicit Boolean(std::string_view inVal);

Boolean(bool val);
Boolean(bool inVal);

std::string toString() const override;

Expand All @@ -24,7 +24,7 @@ class Boolean final : public ILiteralCRTP<Boolean> {
}

private:
std::string name;
std::string_view val;
};

}
5 changes: 3 additions & 2 deletions include/fintamath/literals/Variable.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <string_view>

#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/literals/ILiteral.hpp"
Expand All @@ -10,9 +11,9 @@ namespace fintamath {

class Variable final : public ILiteralCRTP<Variable> {
public:
explicit Variable(std::string inName);
explicit Variable(std::string_view inName);

explicit Variable(std::string inName, Integer inIndex);
explicit Variable(std::string_view inName, Integer inIndex);

std::string toString() const override;

Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/numbers/IInteger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <memory>
#include <string>

#include <fmt/core.h>

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
Expand Down
8 changes: 7 additions & 1 deletion include/fintamath/numbers/IIntegerCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ class IIntegerCRTP_ : public IInteger {
return cast<IInteger>(res->toMinimalObject());
}

throw InvalidInputBinaryOperatorException(operStr, toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of "{}" operator ({} "{}" and {} "{}" are unconvertible to each other))",
operStr,
this->getType().getName(),
this->toString(),
rhs.getType().getName(),
rhs.toString()));
}

private:
Expand Down
3 changes: 2 additions & 1 deletion include/fintamath/numbers/Integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include <boost/container_hash/hash.hpp>
#include <boost/multiprecision/fwd.hpp>
Expand All @@ -26,7 +27,7 @@ class Integer final : public IIntegerCRTP<Integer> {

Integer(Backend inBackend);

explicit Integer(std::string str);
explicit Integer(std::string_view str);

explicit Integer(std::integral auto val) : backend(val) {
}
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/numbers/Rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Rational final : public INumberCRTP<Rational> {
public:
Rational() = default;

explicit Rational(const std::string &str);
explicit Rational(std::string_view str);

explicit Rational(Integer inNumer, Integer inDenom);

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/numbers/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include <boost/container_hash/hash.hpp>
#include <boost/multiprecision/fwd.hpp>
#include <boost/multiprecision/mpfr.hpp>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IComparable.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/numbers/INumber.hpp"
#include "fintamath/numbers/Integer.hpp"
Expand Down Expand Up @@ -42,7 +42,7 @@ class Real final : public INumberCRTP<Real> {

Real(Backend inBackend);

explicit Real(std::string str);
explicit Real(std::string_view str);

Real(const Rational &val);

Expand Down
Loading

0 comments on commit 00b4f65

Please sign in to comment.