From 027c13e8fcf3c3c606287f5c229740d3b1671cde Mon Sep 17 00:00:00 2001 From: Iblis Lin Date: Mon, 23 Sep 2019 05:21:25 +0800 Subject: [PATCH] julia: add `AbstractMXError` as parent type (#16235) --- julia/NEWS.md | 3 +++ julia/src/MXNet.jl | 5 ++++ julia/src/base.jl | 7 ------ julia/src/exceptions.jl | 31 +++++++++++++++++++++++ julia/test/unittest/exceptions.jl | 42 +++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 julia/src/exceptions.jl create mode 100644 julia/test/unittest/exceptions.jl diff --git a/julia/NEWS.md b/julia/NEWS.md index e678b97176d1..7e3076a303d4 100644 --- a/julia/NEWS.md +++ b/julia/NEWS.md @@ -17,6 +17,9 @@ # v1.6.0 +* Add an abstract type `AbstractMXError` as the parent type for all MXNet-related + API errors. (#16235) + # v1.5.0 diff --git a/julia/src/MXNet.jl b/julia/src/MXNet.jl index 5a146396642b..69bbcf85180d 100644 --- a/julia/src/MXNet.jl +++ b/julia/src/MXNet.jl @@ -43,6 +43,10 @@ import Base.Iterators: filter # exports ############################################################################### +# exceptions.jl +export AbstractMXError, + MXError + # symbolic-node.jl export SymbolicNode, Variable, @@ -136,6 +140,7 @@ export to_graphviz # includes ############################################################################### +include("exceptions.jl") include("base.jl") include("runtime.jl") diff --git a/julia/src/base.jl b/julia/src/base.jl index 683146402620..2b60dca4f783 100644 --- a/julia/src/base.jl +++ b/julia/src/base.jl @@ -15,13 +15,6 @@ # specific language governing permissions and limitations # under the License. -"Exception thrown when an error occurred calling MXNet API." -struct MXError <: Exception - msg :: AbstractString -end - -Base.show(io::IO, e::MXError) = print(io, e.msg) - ################################################################################ # Common types used in MXNet API ################################################################################ diff --git a/julia/src/exceptions.jl b/julia/src/exceptions.jl new file mode 100644 index 000000000000..9faa2f8c189e --- /dev/null +++ b/julia/src/exceptions.jl @@ -0,0 +1,31 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +""" +Exception thrown when an error occurred calling MXNet API. +""" +abstract type AbstractMXError <: Exception end + +"General MXNet API error" +struct MXError <: AbstractMXError + msg::String + + MXError(s::AbstractString) = new(string(s)) +end + +Base.show(io::IO, e::AbstractMXError) = print(io, e.msg) diff --git a/julia/test/unittest/exceptions.jl b/julia/test/unittest/exceptions.jl new file mode 100644 index 000000000000..ad952c6483f5 --- /dev/null +++ b/julia/test/unittest/exceptions.jl @@ -0,0 +1,42 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +module TestExceptions + +using MXNet +using Test + +struct MXError′ <: AbstractMXError + msg::String +end + +function test_show() + @info "AbstractMXError::Base.show" + + io = IOBuffer() + e = MXError′("magic") + print(io, e) + str = String(take!(io)) + @test str == "magic" +end + +@testset "Exception Test" begin + test_show() +end + + +end # module TestExceptions