2016-08-31 04:19:25 -04:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2016, Project OSRM contributors
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-01-28 11:41:56 -05:00
|
|
|
#ifndef OSRM_EXCEPTION_HPP
|
|
|
|
#define OSRM_EXCEPTION_HPP
|
2015-01-27 11:44:46 -05:00
|
|
|
|
2017-05-27 00:16:20 -04:00
|
|
|
#include <array>
|
2015-01-27 11:44:46 -05:00
|
|
|
#include <exception>
|
2017-05-27 00:16:20 -04:00
|
|
|
#include <iostream>
|
2015-01-27 11:44:46 -05:00
|
|
|
#include <string>
|
2015-08-18 06:56:34 -04:00
|
|
|
#include <utility>
|
2015-01-27 11:44:46 -05:00
|
|
|
|
2017-05-27 00:16:20 -04:00
|
|
|
#include "osrm/error_codes.hpp"
|
2017-01-19 09:14:30 -05:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
|
2017-05-27 00:16:20 -04:00
|
|
|
class exception : public std::exception
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit exception(const char *message) : message(message) {}
|
2015-08-18 06:56:34 -04:00
|
|
|
explicit exception(std::string message) : message(std::move(message)) {}
|
2017-01-19 09:14:30 -05:00
|
|
|
explicit exception(boost::format message) : message(message.str()) {}
|
2016-02-16 13:51:04 -05:00
|
|
|
const char *what() const noexcept override { return message.c_str(); }
|
2015-01-27 11:44:46 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
// This function exists to 'anchor' the class, and stop the compiler from
|
|
|
|
// copying vtable and RTTI info into every object file that includes
|
|
|
|
// this header. (Caught by -Wweak-vtables under Clang.)
|
|
|
|
virtual void anchor() const;
|
|
|
|
const std::string message;
|
|
|
|
};
|
2017-05-27 00:16:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates a class of error that occurred that was caused by some kind of
|
|
|
|
* external input (common examples are out of disk space, file permission errors,
|
|
|
|
* user supplied bad data, etc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
constexpr const std::array<const char *, 11> ErrorDescriptions = {{
|
|
|
|
"", // Dummy - ErrorCode values start at 2
|
|
|
|
"", // Dummy - ErrorCode values start at 2
|
|
|
|
"Fingerprint did not match the expected value", // InvalidFingerprint
|
|
|
|
"File is incompatible with this version of OSRM", // IncompatibleFileVersion
|
|
|
|
"Problem opening file", // FileOpenError
|
|
|
|
"Problem reading from file", // FileReadError
|
|
|
|
"Problem writing to file", // FileWriteError
|
|
|
|
"I/O error occurred", // FileIOError
|
|
|
|
"Unexpected end of file", // UnexpectedEndOfFile
|
|
|
|
"The dataset you are trying to load is not " // IncompatibleDataset
|
|
|
|
"compatible with the routing algorithm you want to use." // ...continued...
|
|
|
|
"Incompatible algorithm" // IncompatibleAlgorithm
|
|
|
|
}};
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Check that we have messages for every enum
|
|
|
|
static_assert(ErrorDescriptions.size() == ErrorCode::__ENDMARKER__,
|
|
|
|
"ErrorCode list and ErrorDescription lists are different sizes");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class RuntimeError : public exception
|
|
|
|
{
|
|
|
|
using Base = exception;
|
|
|
|
using Base::Base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit RuntimeError(const std::string &message,
|
|
|
|
const ErrorCode code_,
|
|
|
|
const std::string &sourceref,
|
|
|
|
const char *possiblecause = nullptr)
|
|
|
|
: Base(BuildMessage(message, code_, sourceref, possiblecause)), code(code_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode GetCode() const { return code; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// This function exists to 'anchor' the class, and stop the compiler from
|
|
|
|
// copying vtable and RTTI info into every object file that includes
|
|
|
|
// this header. (Caught by -Wweak-vtables under Clang.)
|
|
|
|
virtual void anchor() const;
|
|
|
|
const ErrorCode code;
|
|
|
|
|
|
|
|
static std::string BuildMessage(const std::string &message,
|
|
|
|
const ErrorCode code_,
|
|
|
|
const std::string &sourceref,
|
|
|
|
const char *possiblecause = nullptr)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
result += ErrorDescriptions[code_];
|
|
|
|
result += ": " + std::string(message);
|
|
|
|
result += possiblecause != nullptr
|
|
|
|
? (std::string(" (possible cause: \"") + possiblecause + "\")")
|
|
|
|
: "";
|
|
|
|
result += " (at " + sourceref + ")";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
2015-01-27 11:44:46 -05:00
|
|
|
}
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
|
2016-01-28 11:41:56 -05:00
|
|
|
#endif /* OSRM_EXCEPTION_HPP */
|