From bf7022a2234eda934afb491d683ac5b04caf9286 Mon Sep 17 00:00:00 2001 From: Robert Bryce Date: Tue, 30 May 2023 15:59:38 -0500 Subject: [PATCH] Work to address https://github.com/WISE-Developers/Project_issues/issues/108#issuecomment-1349707648 Changed some exception handling catch blocks for more clarity on the class of the exception being thrown. --- WISE/cpp/Initialization.cpp | 1 + WISE_Project/cpp/CWFGMProject.Serialize.cpp | 1 + WISE_Project/cpp/CWFGMProject.cpp | 14 +++----- WISE_Project/cpp/CWFGMProject.proto.cpp | 38 +++++++++------------ WISE_Project/cpp/GridFilter.proto.cpp | 1 + WISE_Project/cpp/Scenario.proto.cpp | 2 +- 6 files changed, 25 insertions(+), 32 deletions(-) diff --git a/WISE/cpp/Initialization.cpp b/WISE/cpp/Initialization.cpp index 62ceb46..1f78789 100644 --- a/WISE/cpp/Initialization.cpp +++ b/WISE/cpp/Initialization.cpp @@ -128,6 +128,7 @@ const struct _error_msg _error_msgs[] = { { ERROR_WEATHER_STREAM_ATTEMPT_OVERWRITE, "Attempt to overwrite existing data in the weather stream failed." }, { WARNING_WEATHER_STREAM_INTERPOLATE, "There were missing hours in the imported data that were filled using interpolation." }, { WARNING_WEATHER_STREAM_INTERPOLATE_BEFORE_INVALID_DATA, "There were missing hours in the imported data that were filled using interpolation, before encountering invalid data." }, + { ERROR_START_AFTER_NOON | ERROR_SEVERITY_WARNING, "Weather stream cannot start after noon." }, { ERROR_ATTEMPT_EXPORT_EMPTY_FIRE, "Cannot export empty fire perimeter." }, diff --git a/WISE_Project/cpp/CWFGMProject.Serialize.cpp b/WISE_Project/cpp/CWFGMProject.Serialize.cpp index 9bcbaec..9b52cd9 100644 --- a/WISE_Project/cpp/CWFGMProject.Serialize.cpp +++ b/WISE_Project/cpp/CWFGMProject.Serialize.cpp @@ -134,6 +134,7 @@ const struct Project::_error_msg _error_msgs[] = { { ERROR_GRID_WEATHER_NOT_IMPLEMENTED, "An object associated with a simulation which can handle weather data is missing." }, { ERROR_GRID_WEATHER_INVALID_DATES, "If the times for the weather don't work with the scenario start and end times." }, { ERROR_PROJECTION_UNKNOWN, "Cannot parse the provided projection file." }, + { ERROR_TIMEZONE_UNKNOWN, "Cannot determine timezone." }, { ERROR_GRID_PRIMARY_STREAM_UNSPECIFIED, "Multiple weather streams exist, but none have been identified as the primary weather stream." }, { ERROR_GRID_WEATHERSTATIONS_TOO_CLOSE, "At least 2 weather stations are within 100m of each other." }, diff --git a/WISE_Project/cpp/CWFGMProject.cpp b/WISE_Project/cpp/CWFGMProject.cpp index a4241f1..a87a4d5 100644 --- a/WISE_Project/cpp/CWFGMProject.cpp +++ b/WISE_Project/cpp/CWFGMProject.cpp @@ -558,8 +558,6 @@ HRESULT Project::CWFGMProject::SetTimeZone(std::uint32_t timezoneId) if (!timezone) return E_INVALIDARG; - HRESULT hr; - PolymorphicAttribute var(timezoneId); WTimeSpan diff = m_timeManager->m_worldLocation.m_timezone() - timezone->m_timezone; WorldLocation& worldLocation = (WorldLocation&)(m_timeManager->m_worldLocation); worldLocation.SetTimeZoneOffset(timezoneId); @@ -587,8 +585,6 @@ HRESULT Project::CWFGMProject::SetTimeZone(std::uint32_t timezoneId) HRESULT Project::CWFGMProject::SetTimeZoneTS(const WTimeSpan &Timezone) { WTimeSpan diff = m_timeManager->m_worldLocation.m_timezone() - Timezone; - PolymorphicAttribute var(Timezone); - HRESULT hr; WorldLocation& worldLocation = (WorldLocation&)(m_timeManager->m_worldLocation); worldLocation.m_timezone(Timezone); @@ -1313,11 +1309,11 @@ static UINT AFX_CDECL stepBuildGrid(APTR parameter) startUnits = 0; break; case PARA_WS: - value = std::max(0.0, min(200.0, wx.WindSpeed)); + value = std::max(0.0, std::min(200.0, wx.WindSpeed)); startUnits = STORAGE_FORMAT_KM | STORAGE_FORMAT_HOUR; break; case PARA_GUST: - value = std::max(0.0, min(200.0, wx.WindGust)); + value = std::max(0.0, std::min(200.0, wx.WindGust)); startUnits = STORAGE_FORMAT_KM | STORAGE_FORMAT_HOUR; break; case PARA_PRECIP: @@ -1472,11 +1468,11 @@ static UINT AFX_CDECL stepBuildGrid(APTR parameter) startUnits = 0; break; case PARA_WS: - value = std::max(0.0, min(200.0, wx.WindSpeed)); + value = std::max(0.0, std::min(200.0, wx.WindSpeed)); startUnits = STORAGE_FORMAT_KM | STORAGE_FORMAT_HOUR; break; case PARA_GUST: - value = std::max(0.0, min(200.0, wx.WindGust)); + value = std::max(0.0, std::min(200.0, wx.WindGust)); startUnits = STORAGE_FORMAT_KM | STORAGE_FORMAT_HOUR; break; case PARA_PRECIP: @@ -3922,7 +3918,7 @@ HRESULT Project::CWFGMProject::PrintReportTxt(const TCHAR *szPath, const PrintRe resultString += "Active Fuel Types :\n"; std::string moddedResult = "\nActive Fuel Types (modified):\n"; - UCHAR count, unique_count, ii; + UCHAR count, unique_count; long ASCII_index, tmp, export_index; ICWFGM_Fuel *fuel = NULL; diff --git a/WISE_Project/cpp/CWFGMProject.proto.cpp b/WISE_Project/cpp/CWFGMProject.proto.cpp index 0769c02..2092013 100644 --- a/WISE_Project/cpp/CWFGMProject.proto.cpp +++ b/WISE_Project/cpp/CWFGMProject.proto.cpp @@ -409,13 +409,15 @@ HRESULT Project::CWFGMProject::deserialize(const std::string& filename, std::uin file_major_version = data->project().version(); retval = S_OK; } + catch (const ISerializeProto::DeserializeError& de) + { + std::cerr << "Error deserializing protobuf " << de.what() << std::endl; + retval = de.hr; + } catch (std::exception &e) { - auto ee = dynamic_cast_assert(&e); - if ((ee) && (ee->hr != 0)) - retval = ee->hr; - else - retval = ERROR_FILE_FORMAT_INVALID | ERROR_SEVERITY_WARNING; + std::cerr << "Error deserializing protobuf " << e.what() << std::endl; + retval = ERROR_FILE_FORMAT_INVALID | ERROR_SEVERITY_WARNING; } } if (result) @@ -690,9 +692,6 @@ auto Project::CWFGMProject::deserialize(const google::protobuf::Message& proto, m_weatherCollection.AssignNewGrid(m_gridEngine.get()); m_scenarioCollection.AssignNewGrid(oldgrid.get(), m_gridEngine.get()); - PolymorphicAttribute attr; - double temp; - if (project->has_vectors()) if (!m_vectorCollection.deserialize(project->vectors(), myValid, "vectors")) retval = nullptr; @@ -1890,10 +1889,6 @@ void Project::Fuel::PrintReportChanges(FuelCollection* m_fuelCollection, void* m if (!defaultFuel) // can happen if m_csLUTFileName is empty defaultFuel = FindDefault(m_fuelCollection->m_fuelCollection_Canada, m_fuelCollection->m_fuelCollection_NewZealand, m_fuelCollection->m_fuelCollection_Tasmania); - //Check for differences and build both the unmodified and modified lists - UCHAR count, unique_count, ii; - long ASCII_index, export_index, tmp; - //Get the full fuel name Fuel* f = Fuel::FromCOM(this->m_fuel.get()); @@ -2523,7 +2518,7 @@ auto Project::FGMOutputs::deserialize(const google::protobuf::Message& proto, st /// user valid->add_child_validation("WISE.ProjectProto.Project.Outputs.SummaryOutput", strprintf("summaries[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto.version())); weak_assert(false); - throw std::invalid_argument("StatsOutputs: Version is invalid"); + throw ISerializeProto::DeserializeError("StatsOutputs: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } auto vt2 = validation::conditional_make_object(valid, "WISE.ProjectProto.Project.Outputs.SummaryOutput", strprintf("seasonal[%d]", i)); @@ -2621,7 +2616,7 @@ auto Project::FGMOutputs::deserialize(const google::protobuf::Message& proto, st /// user valid->add_child_validation("WISE.ProjectProto.Project.Outputs.StatsOutput", strprintf("stats[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto.version())); weak_assert(false); - throw std::invalid_argument("StatsOutputs: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.StatsOutputs: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } /// @@ -2764,7 +2759,7 @@ HSS_PRAGMA_WARNING_POP /// user statsValid->add_child_validation("WISE.ProjectProto.Project.Outputs.DiscretizedStatsOptions", strprintf("grids[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto.version())); weak_assert(false); - throw std::invalid_argument("DiscretizedStatsOptions: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.DiscretizedStatsOptions: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } so.discretize = (std::uint16_t)dopt.discretize(); if ((so.discretize < 1) || (so.discretize > 1000)) @@ -2822,7 +2817,7 @@ HSS_PRAGMA_WARNING_POP /// user statsValid->add_child_validation("WISE.ProjectProto.Project.Outputs.DiscretizedStatsOptions", strprintf("grids[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto.version())); weak_assert(false); - throw std::invalid_argument("DiscretizedStatsOptions: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.DiscretizedStatsOptions: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } so.discretize = (std::uint16_t)dopt.discretize(); if ((so.discretize < 1) || (so.discretize > 1000)) @@ -2858,7 +2853,7 @@ HSS_PRAGMA_WARNING_POP /// user valid->add_child_validation("WISE.ProjectProto.Project.Outputs.VectorOutput", strprintf("vectors[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto1.version())); weak_assert(false); - throw std::invalid_argument("StatsOutputs: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.VectorOutput: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } /// @@ -3034,7 +3029,7 @@ HSS_PRAGMA_WARNING_POP /// user valid->add_child_validation("WISE.ProjectProto.Project.Outputs.FuelGridOutput", strprintf("fuelgrids[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto1.version())); weak_assert(false); - throw std::invalid_argument("StatsOutputs: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.FuelGridOutput: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } /// @@ -3136,7 +3131,7 @@ HSS_PRAGMA_WARNING_POP /// user valid->add_child_validation("WISE.ProjectProto.Project.Outputs.GridOutput", strprintf("grids[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto.version())); weak_assert(false); - throw std::invalid_argument("StatsOutputs: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.GridOutput: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } /// @@ -3291,7 +3286,7 @@ HSS_PRAGMA_WARNING_POP /// user gridValid->add_child_validation("WISE.ProjectProto.Project.Outputs.DiscretizedStatsOptions", strprintf("grids[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(proto.version())); weak_assert(false); - throw std::invalid_argument("DiscretizedStatsOptions: Unknown version"); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.Project.Outputs.DiscretizedStatsOptions: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } grid.discretize = (std::uint16_t)dopt.discretize(); if ((grid.discretize < 1) || (grid.discretize > 1000)) @@ -3424,7 +3419,7 @@ HSS_PRAGMA_WARNING_POP /// user valid->add_child_validation("WISE.ProjectProto.Project.Outputs.AssetStatsOutput", strprintf("assetStats[%d]", i), validation::error_level::SEVERE, validation::id::version_mismatch, std::to_string(asset.version())); weak_assert(false); - throw std::invalid_argument("AssetOutput: Unknown version"); + throw ISerializeProto::DeserializeError("AssetStatsOutput: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID); } /// @@ -3456,7 +3451,6 @@ HSS_PRAGMA_WARNING_POP entry.criticalPath = asset.has_criticalpathembedded() && asset.criticalpathembedded().value(); entry.criticalPathFilename = asset.criticalpathfilename(); - stats::StatsFileType filetype; switch (asset.filetype()) { case WISE::ProjectProto::Project_Outputs_StatsOutput::CSV: diff --git a/WISE_Project/cpp/GridFilter.proto.cpp b/WISE_Project/cpp/GridFilter.proto.cpp index b4945d2..e67714d 100644 --- a/WISE_Project/cpp/GridFilter.proto.cpp +++ b/WISE_Project/cpp/GridFilter.proto.cpp @@ -737,6 +737,7 @@ auto Project::StaticVector::deserialize(const google::protobuf::Message& proto, /// /// user v->add_child_validation("string", "name", validation::error_level::SEVERE, validation::id::missing_name, vector->name()); + throw ISerializeProto::DeserializeError("CWFGM.ProjectProto.ProjectVectorFilter: Invalid or missing name."); } m_comments = vector->comments(); m_col = vector->color(); diff --git a/WISE_Project/cpp/Scenario.proto.cpp b/WISE_Project/cpp/Scenario.proto.cpp index 9ac9a8c..93e517b 100644 --- a/WISE_Project/cpp/Scenario.proto.cpp +++ b/WISE_Project/cpp/Scenario.proto.cpp @@ -47,7 +47,7 @@ google::protobuf::Message* Project::Scenario::serialize(const SerializeProtoOpti if (!data) { weak_assert(false); - throw std::invalid_argument(""); + throw std::invalid_argument("Scenario: Parameter invalid"); } if (options.fileVersion() == 1) {