2022-03-04 12:29:33 +01:00
|
|
|
//---
|
|
|
|
// fxos-shell.errors: Exceptions with particular handling
|
|
|
|
//---
|
|
|
|
|
|
|
|
#ifndef FXOS_ERRORS_H
|
|
|
|
#define FXOS_ERRORS_H
|
|
|
|
|
2022-03-27 14:59:49 +02:00
|
|
|
#include <fxos/util/format.h>
|
2022-03-04 12:29:33 +01:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2022-03-27 14:59:49 +02:00
|
|
|
/* 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;
|
|
|
|
};
|
|
|
|
|
2022-03-04 12:29:33 +01:00
|
|
|
#endif /* FXOS_ERRORS_H */
|