Skip to content

Commit

Permalink
Merge pull request #222 from fktn-k/feature/214_emit_more_descriptive…
Browse files Browse the repository at this point in the history
…_errors

#214 Emit more descriptive errors
  • Loading branch information
fktn-k committed Nov 23, 2023
2 parents 00d0433 + c3e65d2 commit c374445
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 273 deletions.
16 changes: 12 additions & 4 deletions docs/mkdocs/docs/api/exception/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ class exception : public std::exception;
A basic exception class used in the fkYAML library.
## Derived Classes
| Type | Description |
| --------------------------------------- | ---------------------------------------------------- |
| [invalid_encoding](invalid_encoding.md) | The exception indicating an encoding error. |
| [parse_error](parse_error.md) | The exception indicating an error in parsing. |
| [type_error](type_error.md) | The exception indicating an invalid type conversion. |
## Member Functions
### Construction/Destruction
| Type | Description |
|---------------------------------|--------------------------|
| Name | Description |
| ------------------------------- | ------------------------ |
| [(constructor)](constructor.md) | constructs an exception. |
| [(destructor)](destructor.md) | destroys an exception. |
### Operation
| Type | Description |
|-----------------|--------------------------------------------|
| Name | Description |
| --------------- | ------------------------------------------ |
| [what](what.md) | provides an error message for a exception. |
19 changes: 19 additions & 0 deletions docs/mkdocs/docs/api/exception/invalid_encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<small>Defined in header [`<fkYAML/exception.hpp>`](https://github.com/fktn-k/fkYAML/blob/develop/include/fkYAML/exception.hpp)</small>

# <small>fkyaml::</small>invalid_encoding

```cpp
class invalid_encoding : public exception;
```

A exception class indicating an encoding error.
This class extends the [`fkyaml::exception`](index.md) class and the [`what()`](what.md) function emits an error message in the following format.

```
invalid_encoding: [error message] in=[array of elements detected an error]
```

## **See Also**

* [exception](index.md)
* [what](what.md)
19 changes: 19 additions & 0 deletions docs/mkdocs/docs/api/exception/parse_error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<small>Defined in header [`<fkYAML/exception.hpp>`](https://github.com/fktn-k/fkYAML/blob/develop/include/fkYAML/exception.hpp)</small>

# <small>fkyaml::</small>parse_error

```cpp
class parse_error : public exception;
```

A exception class indicating an error in parsing.
This class extends the [`fkyaml::exception`](index.md) class and the [`what()`](what.md) function emits an error message in the following format.

```
parse_error: [error message] (at line [LINE], column [COLUMN])
```

## **See Also**

* [exception](index.md)
* [what](what.md)
19 changes: 19 additions & 0 deletions docs/mkdocs/docs/api/exception/type_error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<small>Defined in header [`<fkYAML/exception.hpp>`](https://github.com/fktn-k/fkYAML/blob/develop/include/fkYAML/exception.hpp)</small>

# <small>fkyaml::</small>type_error

```cpp
class type_error : public exception;
```

A exception class indicating an invalid type conversion.
This class extends the [`fkyaml::exception`](index.md) class and the [`what()`](what.md) function emits an error message in the following format.

```
type_error: [error message] type=[source node type]
```

## **See Also**

* [exception](index.md)
* [what](what.md)
3 changes: 3 additions & 0 deletions docs/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ nav:
- (constructor): api/exception/constructor.md
- (destructor): api/exception/destructor.md
- what: api/exception/what.md
- invalid_encoding: api/exception/invalid_encoding.md
- parse_error: api/exception/parse_error.md
- type_error: api/exception/type_error.md
- macros: api/macros.md
- node_value_converter:
- node_value_converter: api/node_value_converter/index.md
Expand Down
20 changes: 10 additions & 10 deletions include/fkYAML/detail/conversions/from_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::sequence_t
{
if (!n.is_sequence())
{
throw exception("The target node value type is not sequence type.");
throw type_error("The target node value type is not sequence type.", n.type());
}
s = n.template get_value_ref<const typename BasicNodeType::sequence_type&>();
}
Expand All @@ -70,7 +70,7 @@ inline void from_node(const BasicNodeType& n, std::vector<CompatibleValueType>&
{
if (!n.is_sequence())
{
throw exception("The target node value is not sequence type.");
throw type_error("The target node value is not sequence type.", n.type());
}

s.reserve(n.size());
Expand All @@ -90,7 +90,7 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::mapping_ty
{
if (!n.is_mapping())
{
throw exception("The target node value type is not mapping type.");
throw type_error("The target node value type is not mapping type.", n.type());
}

for (auto pair : n.template get_value_ref<const typename BasicNodeType::mapping_type&>())
Expand All @@ -109,7 +109,7 @@ inline void from_node(const BasicNodeType& n, std::nullptr_t& null)
// to ensure the target node value type is null.
if (!n.is_null())
{
throw exception("The target node value type is not null type.");
throw type_error("The target node value type is not null type.", n.type());
}
null = nullptr;
}
Expand All @@ -123,7 +123,7 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_ty
{
if (!n.is_boolean())
{
throw exception("The target node value type is not boolean type.");
throw type_error("The target node value type is not boolean type.", n.type());
}
b = n.template get_value_ref<const typename BasicNodeType::boolean_type&>();
}
Expand All @@ -137,7 +137,7 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::integer_ty
{
if (!n.is_integer())
{
throw exception("The target node value type is not integer type.");
throw type_error("The target node value type is not integer type.", n.type());
}
i = n.template get_value_ref<const typename BasicNodeType::integer_type&>();
}
Expand All @@ -158,7 +158,7 @@ inline void from_node(const BasicNodeType& n, IntegerType& i)
{
if (!n.is_integer())
{
throw exception("The target node value type is not integer type.");
throw type_error("The target node value type is not integer type.", n.type());
}

// under/overflow check.
Expand All @@ -185,7 +185,7 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::float_numb
{
if (!n.is_float_number())
{
throw exception("The target node value type is not float number type.");
throw type_error("The target node value type is not float number type.", n.type());
}
f = n.template get_value_ref<const typename BasicNodeType::float_number_type&>();
}
Expand All @@ -206,7 +206,7 @@ inline void from_node(const BasicNodeType& n, FloatType& f)
{
if (!n.is_float_number())
{
throw exception("The target node value type is not float number type.");
throw type_error("The target node value type is not float number type.", n.type());
}

auto tmp_float = n.template get_value_ref<const typename BasicNodeType::float_number_type&>();
Expand All @@ -231,7 +231,7 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::string_typ
{
if (!n.is_string())
{
throw exception("The target node value type is not string type.");
throw type_error("The target node value type is not string type.", n.type());
}
s = n.template get_value_ref<const typename BasicNodeType::string_type&>();
}
Expand Down
4 changes: 2 additions & 2 deletions include/fkYAML/detail/encodings/utf8_encoding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class utf8_encoding

if (!is_valid)
{
throw fkyaml::exception("Invalid UTF-16 encoding detected.");
throw fkyaml::invalid_encoding("Invalid UTF-16 encoding detected.", utf16);
}
}

Expand Down Expand Up @@ -315,7 +315,7 @@ class utf8_encoding

if (!is_valid)
{
throw fkyaml::exception("Invalid UTF-32 encoding detected.");
throw fkyaml::invalid_encoding("Invalid UTF-32 encoding detected.", utf32);
}
}
};
Expand Down

0 comments on commit c374445

Please sign in to comment.