Commit d5f43f97 authored by Steven Murray's avatar Steven Murray
Browse files

Added the Serrnum class

parent 2d641596
......@@ -11,6 +11,7 @@ set (COMMON_LIB_SRC_FILES
exception/Backtrace.cpp
exception/Errnum.cpp
exception/Exception.cpp
exception/Serrnum.cpp
RemoteFileStatus.cpp
RemotePath.cpp
RemotePathAndStatus.cpp
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/exception/Serrnum.hpp"
#include "common/Utils.hpp"
#include <errno.h>
#include <string.h>
using namespace cta::exception;
Serrnum::Serrnum(std::string what):Exception("") {
m_serrnum = errno;
SerrnumConstructorBottomHalf(what);
}
Serrnum::Serrnum(int err, std::string what):Exception("") {
m_serrnum = err;
SerrnumConstructorBottomHalf(what);
}
void Serrnum::SerrnumConstructorBottomHalf(const std::string & what) {
m_strerror = Utils::serrnoToString(m_serrnum);
std::stringstream w2;
if (what.size())
w2 << what << " ";
w2 << "Errno=" << m_serrnum << ": " << m_strerror;
getMessage().str(w2.str());
}
void Serrnum::throwOnReturnedErrno (const int err, const std::string &context) {
if (err) throw Serrnum(err, context);
}
void Serrnum::throwOnNonZero(const int status, const std::string &context) {
if (status) throw Serrnum(context);
}
void Serrnum::throwOnZero(const int status, const std::string &context) {
if (!status) throw Serrnum(context);
}
void Serrnum::throwOnNull(const void *const f, const std::string &context) {
if (NULL == f) throw Serrnum(context);
}
void Serrnum::throwOnNegative(const int ret, const std::string &context) {
if (ret < 0) throw Serrnum(context);
}
void Serrnum::throwOnMinusOne(const int ret, const std::string &context) {
if (-1 == ret) throw Serrnum(context);
}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Exception.hpp"
namespace cta {
namespace exception {
class Serrnum: public cta::exception::Exception {
public:
Serrnum(std::string what = "");
Serrnum (int err, std::string what = "");
virtual ~Serrnum() throw() {};
int serrorNumber() const { return m_serrnum; }
std::string strError() const { return m_strerror; }
static void throwOnReturnedErrno(const int err, const std::string &context = "");
static void throwOnNonZero(const int status, const std::string &context = "");
static void throwOnZero(const int status, const std::string &context = "");
static void throwOnNull(const void *const f, const std::string &context = "");
static void throwOnNegative(const int ret, const std::string &context = "");
static void throwOnMinusOne(const int ret, const std::string &context = "");
protected:
void SerrnumConstructorBottomHalf(const std::string & what);
int m_serrnum;
std::string m_strerror;
};
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment