Skip to content

Commit

Permalink
Add custom error creator function
Browse files Browse the repository at this point in the history
This commit add the tg_geom_new_error() function, which allows
for creating custom error geometries outside of the TG library.
  • Loading branch information
tidwall committed Dec 3, 2024
1 parent 017286d commit 1f494a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Functions for creating and freeing geometries.
- [tg_geom_new_multilinestring()](#group___geometry_constructors_1gaccfd80de48f1e5c7de38cbf515d3bf9f)
- [tg_geom_new_multipolygon()](#group___geometry_constructors_1gad57b01099788e21a5103cd8293f4e364)
- [tg_geom_new_geometrycollection()](#group___geometry_constructors_1ga39569ef55606b8d54ed7cf14a4382bdd)
- [tg_geom_new_error()](#group___geometry_constructors_1ga53823d8d5f2f77ba2ac14b4f8a77700c)
- [tg_geom_clone()](#group___geometry_constructors_1ga76f55846aa97188b1840b9ad07dae000)
- [tg_geom_copy()](#group___geometry_constructors_1ga389a890d60908d3e2b448a0e2b3d558e)
- [tg_geom_free()](#group___geometry_constructors_1gaf6f400f624b9f3e9052ac26ab17d72ae)
Expand Down Expand Up @@ -871,6 +872,14 @@ Creates a GeometryCollection geometry.



<a name='group___geometry_constructors_1ga53823d8d5f2f77ba2ac14b4f8a77700c'></a>
## tg_geom_new_error()
```c
struct tg_geom *tg_geom_new_error(const char *errmsg);
```
Utility for returning an error message wrapped in a geometry. This operation does not return a real geometry, only an error message, which may be useful for generating custom errors from operations outside of the TG library.


<a name='group___geometry_constructors_1ga76f55846aa97188b1840b9ad07dae000'></a>
## tg_geom_clone()
```c
Expand Down
16 changes: 16 additions & 0 deletions tests/test_geom.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,21 @@ void test_geom_chaos(void) {

}

void test_geom_error(void) {
struct tg_geom *geom;
geom = tg_geom_new_error(0);
assert(strcmp(tg_geom_error(geom), "no memory") == 0);
tg_geom_free(geom);

geom = tg_geom_new_error("");
assert(strcmp(tg_geom_error(geom), "") == 0);
tg_geom_free(geom);

geom = tg_geom_new_error("hello");
assert(strcmp(tg_geom_error(geom), "hello") == 0);
tg_geom_free(geom);

}

int main(int argc, char **argv) {
do_test(test_geom_point);
Expand All @@ -996,5 +1011,6 @@ int main(int argc, char **argv) {
do_test(test_geom_various);
do_test(test_geom_copy);
do_chaos_test(test_geom_chaos);
do_test(test_geom_error);
return 0;
}
8 changes: 8 additions & 0 deletions tg.c
Original file line number Diff line number Diff line change
Expand Up @@ -14344,3 +14344,11 @@ struct tg_geom *tg_parse_ix(const void *data, size_t len, enum tg_index ix) {
wkb:
return tg_parse_wkb_ix((uint8_t*)src, len, ix);
}

/// Utility for returning an error message wrapped in a geometry.
/// This operation does not return a real geometry, only an error message,
/// which may be useful for generating custom errors from operations
/// outside of the TG library.
struct tg_geom *tg_geom_new_error(const char *error) {
return error?make_parse_error("%s", error):0;
}
1 change: 1 addition & 0 deletions tg.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct tg_geom *tg_geom_new_multipoint(const struct tg_point *points, int npoint
struct tg_geom *tg_geom_new_multilinestring(const struct tg_line *const lines[], int nlines);
struct tg_geom *tg_geom_new_multipolygon(const struct tg_poly *const polys[], int npolys);
struct tg_geom *tg_geom_new_geometrycollection(const struct tg_geom *const geoms[], int ngeoms);
struct tg_geom *tg_geom_new_error(const char *errmsg);
struct tg_geom *tg_geom_clone(const struct tg_geom *geom);
struct tg_geom *tg_geom_copy(const struct tg_geom *geom);
void tg_geom_free(struct tg_geom *geom);
Expand Down

0 comments on commit 1f494a5

Please sign in to comment.