Skip to content

Commit

Permalink
igraph.error function to get exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonov548 committed Jan 7, 2024
1 parent 1988611 commit fbec706
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@
return val{typed_memory_view(size, vector.stor_begin)}; \
}

bool get_directed(const ig::igGraph& graph) {
namespace {

bool get_directed(const ig::igGraph& graph) {
const auto* g{static_cast<const igraph_t*>(graph)};
return g->directed;
}
}

igraph_integer_t get_n(const ig::igGraph& graph) {
igraph_integer_t get_n(const ig::igGraph& graph) {
const auto* g{static_cast<const igraph_t*>(graph)};
return g->n;
}
}

var get_vector(const ig::igIntVec& v) {
var get_vector(const ig::igIntVec& v) {
return var{typed_memory_view(v.size(), v.begin())};
}
}

ig::igIntVec create_vector(var arr) {
ig::igIntVec create_vector(var arr) {
ig::igIntVec v{};
const auto data{convertJSArrayToNumberVector<igraph_integer_t>(arr)};

v.resize(data.size());
for (size_t i{0}; i < data.size(); ++i) v[i] = data[i];

return v;
}

std::string error(std::uintptr_t p) {
return reinterpret_cast<ig::igException*>(p)->what();
}

}

IGRAPH_VECTOR_EMCC(from)
Expand Down Expand Up @@ -65,4 +73,6 @@ EMSCRIPTEN_BINDINGS(IGraphTypes)
value("IN", IGRAPH_STAR_IN).
value("UNDIRECTED", IGRAPH_STAR_UNDIRECTED).
value("MUTUAL", IGRAPH_STAR_MUTUAL);

function("error", &error);
}
6 changes: 6 additions & 0 deletions tests/unit_tests/edges.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ describe('igraph.js', () => {
const v1 = new igraph.vector_int([0, 1, 2]);
expect(() => { igraph.add_edges(g, v1, 0) }).toThrow();

try {
igraph.add_edges(g, v1, 0);
} catch (e) {
expect(igraph.error(e)).toBe('Invalid edge vector');
}

v1.delete();
g.delete();
});
Expand Down

0 comments on commit fbec706

Please sign in to comment.