Skip to content

Commit

Permalink
julia: add AbstractMXError as parent type (apache#16235)
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin authored and larroy committed Sep 28, 2019
1 parent 709b9f3 commit 027c13e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
3 changes: 3 additions & 0 deletions julia/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions julia/src/MXNet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import Base.Iterators: filter
# exports
###############################################################################

# exceptions.jl
export AbstractMXError,
MXError

# symbolic-node.jl
export SymbolicNode,
Variable,
Expand Down Expand Up @@ -136,6 +140,7 @@ export to_graphviz
# includes
###############################################################################

include("exceptions.jl")
include("base.jl")

include("runtime.jl")
Expand Down
7 changes: 0 additions & 7 deletions julia/src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
################################################################################
Expand Down
31 changes: 31 additions & 0 deletions julia/src/exceptions.jl
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions julia/test/unittest/exceptions.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 027c13e

Please sign in to comment.