Skip to content

Commit b1c5e04

Browse files
committed
Better error messages for constructors
1 parent b14cae3 commit b1c5e04

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/metagraph.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ function MetaGraph(
6767
weight_function=edge_data -> 1.0,
6868
default_weight=1.0,
6969
) where {Code,Label,VertexData,EdgeData}
70-
@assert nv(graph) == 0
70+
if nv(graph) != 0
71+
throw(
72+
ArgumentError(
73+
"For this MetaGraph constructor, the underlying graph should be empty."
74+
),
75+
)
76+
end
7177
if Label <: Integer
7278
@warn "Constructing a MetaGraph with integer labels is not advised."
7379
end
@@ -148,19 +154,37 @@ function MetaGraph(
148154
default_weight=1.0,
149155
) where {Code,Label,VertexData,EdgeData}
150156
# Construct vertex data
151-
@assert length(vertices_description) == nv(graph)
157+
if length(vertices_description) != nv(graph)
158+
throw(
159+
ArgumentError(
160+
"For this MetaGraph constructor, the description of vertices should contain as many vertices as the underlying graph.",
161+
),
162+
)
163+
end
152164
vertex_labels = Dict{Code,Label}()
153165
vertex_properties = Dict{Label,Tuple{Code,VertexData}}()
154166
for (code, (label, data)) in enumerate(vertices_description)
155167
vertex_labels[code] = label
156168
vertex_properties[label] = (code, data)
157169
end
158170
# Construct edge data
159-
@assert length(edges_description) == ne(graph)
171+
if length(edges_description) != ne(graph)
172+
throw(
173+
ArgumentError(
174+
"For this MetaGraph constructor, the description of edges should contain as many edges as the underlying graph.",
175+
),
176+
)
177+
end
160178
for ((label_1, label_2), _) in edges_description
161179
code_1 = vertex_properties[label_1][1]
162180
code_2 = vertex_properties[label_2][1]
163-
@assert has_edge(graph, code_1, code_2)
181+
if !has_edge(graph, code_1, code_2)
182+
throw(
183+
ArgumentError(
184+
"For this MetaGraph constructor, each edge in the edge description should exist in the underlying graph.",
185+
),
186+
)
187+
end
164188
end
165189
edge_data = Dict{Tuple{Label,Label},EdgeData}()
166190
for ((label_1, label_2), data) in edges_description

test/tutorial/4_type_stability.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ MetaGraph(cycle_graph(3), vertices_description, edges_description, "additive col
6969
vertices_description, #src
7070
edges_description, #src
7171
"additive colors", #src
72-
)
72+
), #src
7373
) == colors) #src
7474

7575
# Once Julia can infer the full type of the `MetaGraph`, accessing vertex and edge metadata also becomes type-stable.

0 commit comments

Comments
 (0)