Skip to content

Commit 032b30e

Browse files
K20shoresclaude
andauthored
Errors (#948)
* initial pass at refactoring errors * adding base exception * using a single class for an exception * removing internal error * correcting cuda build errors * Address PR review comments on MicmException - Change category_ member from string_view to const char* to avoid dangling reference risk when passing temporaries - Add direct include of micm_exception.hpp to sparse_matrix.hpp and sparse_matrix_standard_ordering_compressed_sparse_row.hpp - Fix matrix.hpp bug where constructed msg was dropped (passed "" instead) - Fill in all empty exception message strings across matrix, sparse matrix, constraint, rate constant, and state throw sites Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ad00ab7 commit 032b30e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+363
-819
lines changed

include/micm/Util.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
#include <micm/util/constants.hpp>
77
#include <micm/util/error.hpp>
8-
#include <micm/util/internal_error.hpp>
8+
#include <micm/util/micm_exception.hpp>
99
#include <micm/util/jacobian.hpp>
1010
#include <micm/util/matrix.hpp>
11-
#include <micm/util/matrix_error.hpp>
1211
#include <micm/util/property_keys.hpp>
1312
#include <micm/util/random_string.hpp>
1413
#include <micm/util/sparse_matrix.hpp>

include/micm/constraint/constraint_error.hpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

include/micm/constraint/constraint_set.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#pragma once
44

55
#include <micm/constraint/constraint.hpp>
6-
#include <micm/constraint/constraint_error.hpp>
6+
#include <micm/util/micm_exception.hpp>
77
#include <micm/util/matrix.hpp>
88
#include <micm/util/sparse_matrix.hpp>
99

@@ -12,8 +12,6 @@
1212
#include <memory>
1313
#include <set>
1414
#include <string>
15-
#include <stdexcept>
16-
#include <system_error>
1715
#include <utility>
1816
#include <vector>
1917

@@ -135,15 +133,20 @@ namespace micm
135133
auto row_it = variable_map.find(algebraic_species);
136134
if (row_it == variable_map.end())
137135
{
138-
throw std::system_error(
139-
make_error_code(MicmConstraintErrc::UnknownSpecies),
136+
throw MicmException(
137+
MicmSeverity::Error,
138+
MICM_ERROR_CATEGORY_CONSTRAINT,
139+
MICM_CONSTRAINT_ERROR_CODE_UNKNOWN_SPECIES,
140140
"Constraint '" + constraint.GetName() + "' targets unknown algebraic species '" + algebraic_species + "'");
141141
}
142142
info.constraint_row_ = row_it->second;
143143

144144
if (!algebraic_variable_ids_.insert(info.constraint_row_).second)
145145
{
146-
throw std::runtime_error(
146+
throw MicmException(
147+
MicmSeverity::Error,
148+
MICM_ERROR_CATEGORY_CONSTRAINT,
149+
MICM_CONSTRAINT_ERROR_CODE_INVALID_STOICHIOMETRY,
147150
"Multiple constraints map to the same algebraic species row '" + algebraic_species + "'");
148151
}
149152

@@ -163,8 +166,10 @@ namespace micm
163166
auto it = variable_map.find(species_name);
164167
if (it == variable_map.end())
165168
{
166-
throw std::system_error(
167-
make_error_code(MicmConstraintErrc::UnknownSpecies),
169+
throw MicmException(
170+
MicmSeverity::Error,
171+
MICM_ERROR_CATEGORY_CONSTRAINT,
172+
MICM_CONSTRAINT_ERROR_CODE_UNKNOWN_SPECIES,
168173
"Constraint '" + constraint.GetName() + "' depends on unknown species '" + species_name + "'");
169174
}
170175
dependency_ids_.push_back(it->second);

include/micm/constraint/equilibrium_constraint.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#pragma once
44

5-
#include <micm/constraint/constraint_error.hpp>
5+
#include <micm/constraint/constraint.hpp>
66
#include <micm/system/stoich_species.hpp>
77

88
#include <cmath>
99
#include <cstddef>
1010
#include <string>
11-
#include <system_error>
1211
#include <vector>
1312

1413
namespace micm
@@ -69,28 +68,28 @@ namespace micm
6968
{
7069
if (reactants_.empty())
7170
{
72-
throw std::system_error(make_error_code(MicmConstraintErrc::EmptyReactants));
71+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_CONSTRAINT, MICM_CONSTRAINT_ERROR_CODE_EMPTY_REACTANTS, "Equilibrium constraint requires at least one reactant");
7372
}
7473
if (products_.empty())
7574
{
76-
throw std::system_error(make_error_code(MicmConstraintErrc::EmptyProducts));
75+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_CONSTRAINT, MICM_CONSTRAINT_ERROR_CODE_EMPTY_PRODUCTS, "Equilibrium constraint requires at least one product");
7776
}
7877
if (equilibrium_constant_ <= 0)
7978
{
80-
throw std::system_error(make_error_code(MicmConstraintErrc::InvalidEquilibriumConstant));
79+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_CONSTRAINT, MICM_CONSTRAINT_ERROR_CODE_INVALID_EQUILIBRIUM_CONSTANT, "Equilibrium constant must be positive");
8180
}
8281
for (const auto& r : reactants_)
8382
{
8483
if (r.coefficient_ <= 0)
8584
{
86-
throw std::system_error(make_error_code(MicmConstraintErrc::InvalidStoichiometry));
85+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_CONSTRAINT, MICM_CONSTRAINT_ERROR_CODE_INVALID_STOICHIOMETRY, "Stoichiometric coefficients must be positive");
8786
}
8887
}
8988
for (const auto& p : products_)
9089
{
9190
if (p.coefficient_ <= 0)
9291
{
93-
throw std::system_error(make_error_code(MicmConstraintErrc::InvalidStoichiometry));
92+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_CONSTRAINT, MICM_CONSTRAINT_ERROR_CODE_INVALID_STOICHIOMETRY, "Stoichiometric coefficients must be positive");
9493
}
9594
}
9695

include/micm/constraint/linear_constraint.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#pragma once
44

5-
#include <micm/constraint/constraint_error.hpp>
5+
#include <micm/util/micm_exception.hpp>
66
#include <micm/system/stoich_species.hpp>
77

88
#include <cstddef>
99
#include <string>
10-
#include <system_error>
1110
#include <vector>
1211

1312
namespace micm
@@ -51,7 +50,7 @@ namespace micm
5150
{
5251
if (terms_.empty())
5352
{
54-
throw std::system_error(make_error_code(MicmConstraintErrc::EmptyReactants));
53+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_CONSTRAINT, MICM_CONSTRAINT_ERROR_CODE_EMPTY_REACTANTS, "");
5554
}
5655
for (const auto& term : terms_)
5756
{

include/micm/process/chemical_reaction.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#pragma once
44

5-
#include <micm/process/process_error.hpp>
65
#include <micm/process/rate_constant/rate_constant.hpp>
76
#include <micm/solver/lu_decomposition.hpp>
87
#include <micm/solver/state.hpp>
@@ -58,8 +57,7 @@ namespace micm
5857
if (this != &other)
5958
{
6059
if (!other.rate_constant_)
61-
throw std::system_error(
62-
make_error_code(MicmProcessErrc::RateConstantIsNotSet),
60+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_PROCESS, MICM_PROCESS_ERROR_CODE_RATE_CONSTANT_IS_NOT_SET,
6361
"Cannot copy from a ChemicalReaction with null rate constant");
6462

6563
reactants_ = other.reactants_;
@@ -101,8 +99,7 @@ namespace micm
10199
void Validate() const
102100
{
103101
if (!rate_constant_)
104-
throw std::system_error(
105-
make_error_code(MicmProcessErrc::RateConstantIsNotSet), "Rate Constant pointer cannot be null");
102+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_PROCESS, MICM_PROCESS_ERROR_CODE_RATE_CONSTANT_IS_NOT_SET, "Rate Constant pointer cannot be null");
106103
}
107104
};
108105

include/micm/process/chemical_reaction_builder.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <micm/process/chemical_reaction.hpp>
66
#include <micm/process/process.hpp>
7-
#include <micm/process/process_error.hpp>
87
#include <micm/system/phase.hpp>
98
#include <micm/system/species.hpp>
109
#include <micm/system/stoich_species.hpp>
@@ -65,8 +64,7 @@ namespace micm
6564
Process Build()
6665
{
6766
if (!rate_constant_)
68-
throw std::system_error(
69-
make_error_code(MicmProcessErrc::RateConstantIsNotSet), "Rate Constant pointer cannot be null.");
67+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_PROCESS, MICM_PROCESS_ERROR_CODE_RATE_CONSTANT_IS_NOT_SET, "Rate Constant pointer cannot be null.");
7068

7169
ChemicalReaction reaction(std::move(reactants_), std::move(products_), std::move(rate_constant_), phase_);
7270
return Process(std::move(reaction));

include/micm/process/phase_transfer_process.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#pragma once
44

5-
#include <micm/process/process_error.hpp>
65
#include <micm/process/transfer_coefficient/transfer_coefficient.hpp>
76
#include <micm/system/phase.hpp>
87
#include <micm/system/species.hpp>
@@ -67,8 +66,7 @@ namespace micm
6766
if (this != &other)
6867
{
6968
if (!other.coefficient_)
70-
throw std::system_error(
71-
make_error_code(MicmProcessErrc::TransferCoefficientIsNotSet),
69+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_PROCESS, MICM_PROCESS_ERROR_CODE_TRANSFER_COEFFICIENT_IS_NOT_SET,
7270
"Cannot copy from a PhaseTransferProcess with null coefficient");
7371

7472
gas_phase_ = other.gas_phase_;
@@ -87,8 +85,7 @@ namespace micm
8785
void Validate() const
8886
{
8987
if (!coefficient_)
90-
throw std::system_error(
91-
make_error_code(MicmProcessErrc::TransferCoefficientIsNotSet),
88+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_PROCESS, MICM_PROCESS_ERROR_CODE_TRANSFER_COEFFICIENT_IS_NOT_SET,
9289
"Phase Transfer Coefficient pointer cannot be null");
9390
}
9491
};

include/micm/process/phase_transfer_process_builder.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ namespace micm
7777
Process Build()
7878
{
7979
if (!coefficient_)
80-
throw std::system_error(
81-
make_error_code(MicmProcessErrc::TransferCoefficientIsNotSet),
80+
throw MicmException(MicmSeverity::Error, MICM_ERROR_CATEGORY_PROCESS, MICM_PROCESS_ERROR_CODE_TRANSFER_COEFFICIENT_IS_NOT_SET,
8281
"Phase Transfer Coefficient pointer cannot be null");
8382

8483
PhaseTransferProcess process(

include/micm/process/process_error.hpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)