Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopendlebury committed Feb 22, 2024
1 parent c9a3b4c commit 898021d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pythonApi/grib_to_arrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../src/exceptions/arrowgenericexception.hpp"
#include "../src/exceptions/invalidcsvexception.hpp"
#include "../src/exceptions/invalidschemaexception.hpp"
#include "../src/exceptions/gribexception.hpp"

//#define USE_CMAKE

Expand Down Expand Up @@ -37,6 +38,7 @@ PYBIND11_MODULE(gribtoarrow, m)
py::register_exception<UnableToCreateArrowTableReaderException>(m, "UnableToCreateArrowTableReaderException");
py::register_exception<ArrowGenericException>(m, "ArrowGenericException");
py::register_exception<InvalidSchemaException>(m, "InvalidSchemaException");
py::register_exception<GribException>(m, "GribException");

py::module::import("pyarrow");
py::class_<GribReader>(m, "GribReader")
Expand Down
9 changes: 9 additions & 0 deletions src/exceptions/gribexception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

class GribException : public std::runtime_error
{
public:

GribException(std::string errorDetails) : std::runtime_error("Exception " + errorDetails) { }

};
39 changes: 35 additions & 4 deletions src/gribmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "caster.hpp"
#include "gribmessage.hpp"
#include "arrowutils.hpp"
#include "exceptions/gribexception.hpp"



Expand Down Expand Up @@ -238,17 +239,40 @@ using namespace std;
string GribMessage::getStringParameter(string parameterName) {
size_t parameterNameLength;
auto parameterNameC = parameterName.c_str();
codes_get_length(h, parameterNameC, &parameterNameLength);
auto err = codes_get_length(h, parameterNameC, &parameterNameLength);
if(err !=0 ) {
std::ostringstream oss;
oss << "Error calling codes_get_length whilst trying to access string key with name " <<
parameterName << "got error code " << err
<< " whilst processing file " << _reader->getFilePath();

throw GribException (oss.str());
}
auto short_name = new char[parameterNameLength];
codes_get_string(h, parameterNameC, short_name, &parameterNameLength);
err = codes_get_string(h, parameterNameC, short_name, &parameterNameLength);
if(err !=0 ) {
std::ostringstream oss;
oss << "Error calling codes_get_string whilst trying to access string key with name " <<
parameterName << "got error code " << err
<< " whilst processing file " << _reader->getFilePath();

throw GribException (oss.str());
}
string short_name_std = short_name;
delete [] short_name;
return short_name_std;
}

long GribMessage::getNumericParameter(string parameterName) {
long parameterId;
codes_get_long(h, parameterName.c_str(), &parameterId);
auto err = codes_get_long(h, parameterName.c_str(), &parameterId);
if(err !=0 ) {
std::ostringstream oss;
oss << "Error calling codes_get_double got error code " << err
<< " whilst processing file " << _reader->getFilePath();

throw GribException (oss.str());
}
return parameterId;
}

Expand All @@ -260,7 +284,14 @@ using namespace std;

double GribMessage::getDoubleParameter(string parameterName) {
double parameterId;
codes_get_double(h, parameterName.c_str(), &parameterId);
auto err = codes_get_double(h, parameterName.c_str(), &parameterId);
if(err !=0 ) {
std::ostringstream oss;
oss << "Error calling codes_get_double got error code " << err
<< " whilst processing file " << _reader->getFilePath();

throw GribException (oss.str());
}
return parameterId;
}

Expand Down
14 changes: 14 additions & 0 deletions src/gribreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "exceptions/arrowgenericexception.hpp"
#include "exceptions/invalidcsvexception.hpp"
#include "exceptions/invalidschemaexception.hpp"
#include "exceptions/gribexception.hpp"

using namespace std;
namespace cp = arrow::compute;
Expand Down Expand Up @@ -340,6 +341,15 @@ Iterator GribReader::begin() {
if (!isExhausted || isRepeatable) {
std::cout << "Creating iterator" << endl;
codes_handle* h = codes_handle_new_from_file(0, fin, PRODUCT_GRIB, &err);
std::cout << "handle is " << h << std::endl;
if(h == nullptr || h == NULL || err != 0) {

std::ostringstream oss;
oss << "Error calling codes_handle_new_from_file got error code " << err
<< " whilst processing file " << filepath;

throw GribException (oss.str());
}
auto m = new GribMessage(this, h, 0l);
return Iterator(this, m, m_endMessage);
} else {
Expand Down Expand Up @@ -484,3 +494,7 @@ arrow::Result<std::shared_ptr<arrow::Array>> GribReader::createSurrogateKeyCol(l
void GribReader::setExhausted(bool status) {
isExhausted = status;
}

std::string GribReader::getFilePath() {
return filepath;
}
1 change: 1 addition & 0 deletions src/gribreader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class GribReader
GribLocationData* addLocationDataToCache(std::unique_ptr<GridArea>& area, GribLocationData* locationData);

void setExhausted(bool status);
std::string getFilePath();

private:
string filepath;
Expand Down
Binary file added tests/meps_weatherapi_sorlandet.grb.bz2
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test_dodgy_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import polars as pl
import pytest


class TestDodgyFile:
def test_compressed_file(self, resource):
import gribtoarrow

#Since the file is compressed externally we can't read it
reader = gribtoarrow.GribReader(str(resource) + "/meps_weatherapi_sorlandet.grb.bz2")

with pytest.raises(gribtoarrow.GribException):
iter(reader)

0 comments on commit 898021d

Please sign in to comment.