Skip to content

Commit

Permalink
Add new exception when file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopendlebury committed Jan 31, 2024
1 parent 7ef3693 commit 427a155
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pythonApi/grib_to_arrow.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../src/gribreader.hpp"
#include "../src/gribmessage.hpp"
#include "../src/exceptions/notsuchfileexception.hpp"

//#define USE_CMAKE

Expand All @@ -24,6 +25,8 @@ namespace py = pybind11;
using namespace std;
PYBIND11_MODULE(gribtoarrow, m)
{

py::register_exception<NoSuchFileException>(m, "NoSuchFileException");
arrow::py::import_pyarrow();
py::class_<GribReader>(m, "GribReader")
.def(py::init<string>(), pybind11::call_guard<pybind11::gil_scoped_release>(), R"EOL(
Expand Down
22 changes: 22 additions & 0 deletions src/exceptions/notsuchfileexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <exception>
#include <string>

using namespace std;

class NoSuchFileException : public std::exception {

public:

NoSuchFileException(string filepath) : filepath(filepath) {
std::string errType = "Unable to find file ";
errMsg = (errType + filepath).c_str();
};

const char* what() const noexcept {
return errMsg;
}

private:
std::string filepath;
const char* errMsg;
};
2 changes: 2 additions & 0 deletions src/gribreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gribmessageiterator.hpp"
#include "caster.hpp"
#include "converter.hpp"
#include "exceptions/notsuchfileexception.hpp"

using namespace std;
namespace cp = arrow::compute;
Expand All @@ -37,6 +38,7 @@ GribReader::GribReader(string filepath) : filepath(filepath) {
-999l);
fin = fopen(filepath.c_str(), "rb");
if (!fin) {
throw NoSuchFileException(filepath);
cout << "Error: unable to open input file" << filepath << endl;
} else {
cout << "I'm ready file is " << fin << endl;
Expand Down

0 comments on commit 427a155

Please sign in to comment.