Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic exception handling #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/GraphViz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module GraphViz
g.handle = C_NULL
end

struct GraphVizError <: Exception
msg::String
end

Base.showerror(io::IO, e::GraphVizError) = print(io, "GraphVizError: " * e.msg)

function Graph(graph::IO)
iodisc = Ref(Agiodisc_s(
@cfunction(jl_afread,Cint,(Any,Ptr{UInt8},Cint)),
Expand All @@ -81,7 +87,11 @@ module GraphViz
discs = Ref(Agdisc_s(cglobal((:AgMemDisc,libcgraph)),
cglobal((:AgIdDisc,libcgraph)),
Base.unsafe_convert(Ptr{Agiodisc_s}, iodisc)))
Graph(@GC.preserve iodisc ccall((:agread,libcgraph),Ptr{Cvoid},(Any,Ptr{Cvoid}),graph,discs))
graph = Graph(@GC.preserve iodisc ccall((:agread,libcgraph),Ptr{Cvoid},(Any,Ptr{Cvoid}),graph,discs))
if graph.handle == C_NULL
throw(GraphVizError("Bad input"))
end
graph
end
Graph(graph::Vector{UInt8}) = Graph(IOBuffer(graph))
Graph(graph::String) = @GC.preserve graph Graph(unsafe_wrap(Vector{UInt8}, graph))
Expand All @@ -93,6 +103,8 @@ module GraphViz
@assert g.handle != C_NULL
if ccall((:gvLayout,libgvc),Cint,(Ptr{Cvoid},Ptr{Cvoid},Ptr{UInt8}),context.handle,g.handle,engine) == 0
g.didlayout = true
else
throw(GraphVizError("Layout failed"))
end
end

Expand Down