fxos/shell/errors.h

63 lines
1.4 KiB
C++

//---
// fxos-shell.errors: Exceptions with particular handling
//---
#ifndef FXOS_ERRORS_H
#define FXOS_ERRORS_H
#include <fxos/util/format.h>
#include <fmt/core.h>
#include <string>
/* Generic command error, either parsing or execution, which prevents a command
from accomplishing its intended task. */
class CommandError: public std::exception
{
public:
CommandError(std::string what): m_what(what) {}
/* Build directly from format arguments */
template <typename... Args>
CommandError(std::string const &format_str, Args&&... args):
m_what(fmt::format(format_str, args...)) {}
char const *what() const noexcept override {
return m_what.c_str();
}
private:
std::string m_what;
};
/* Syntax errors for commands. */
class SyntaxError: public std::exception
{
public:
/* Specifies the file and line of the exception */
SyntaxError(char const *file, int line, char const *what):
m_file(file), m_line(line), m_what(what) {}
/* Provides access to these objects */
char const *file() const noexcept {
return m_file;
}
int line() const noexcept {
return m_line;
}
char const *what() const noexcept override {
return m_what;
}
/* Additional friendly formatter */
std::string str() const noexcept {
return format("%s:%d: %s", m_file, m_line, m_what);
}
private:
char const *m_file;
int m_line;
char const *m_what;
};
#endif /* FXOS_ERRORS_H */