diff --git a/base/base.jl b/base/base.jl index 909bb062b6a13..0eec65edca597 100644 --- a/base/base.jl +++ b/base/base.jl @@ -7,7 +7,9 @@ end type SystemError <: Exception prefix::AbstractString errnum::Int32 - SystemError(p::AbstractString, e::Integer) = new(p, e) + extrainfo + SystemError(p::AbstractString, e::Integer, extrainfo) = new(p, e, extrainfo) + SystemError(p::AbstractString, e::Integer) = new(p, e, nothing) SystemError(p::AbstractString) = new(p, Libc.errno()) end diff --git a/base/error.jl b/base/error.jl index 4e27de0805c0f..671104d7d3f61 100644 --- a/base/error.jl +++ b/base/error.jl @@ -31,7 +31,7 @@ kwerr(kw) = error("unrecognized keyword argument \"", kw, "\"") ## system error handling ## -systemerror(p, b::Bool) = b ? throw(Main.Base.SystemError(string(p))) : nothing +systemerror(p, b::Bool; extrainfo=nothing) = b ? throw(Main.Base.SystemError(string(p), Libc.errno(), extrainfo)) : nothing ## assertion functions and macros ## diff --git a/base/file.jl b/base/file.jl index afd1ed3652c91..312d511cff60a 100644 --- a/base/file.jl +++ b/base/file.jl @@ -64,7 +64,7 @@ cd(f::Function) = cd(f, homedir()) function mkdir(path::AbstractString, mode::Unsigned=0o777) @unix_only ret = ccall(:mkdir, Int32, (Cstring,UInt32), path, mode) @windows_only ret = ccall(:_wmkdir, Int32, (Cwstring,), path) - systemerror(:mkdir, ret != 0) + systemerror(:mkdir, ret != 0; extrainfo=path) end function mkpath(path::AbstractString, mode::Unsigned=0o777) diff --git a/base/replutil.jl b/base/replutil.jl index 49981475324c6..5d3bad42c0184 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -136,7 +136,13 @@ function showerror(io::IO, ex::DomainError, bt; backtrace=true) nothing end -showerror(io::IO, ex::SystemError) = print(io, "SystemError: $(ex.prefix): $(Libc.strerror(ex.errnum))") +function showerror(io::IO, ex::SystemError) + if ex.extrainfo == nothing + print(io, "SystemError: $(ex.prefix): $(Libc.strerror(ex.errnum))") + else + print(io, "SystemError (with $(ex.extrainfo)): $(ex.prefix): $(Libc.strerror(ex.errnum))") + end +end showerror(io::IO, ::DivideError) = print(io, "DivideError: integer division error") showerror(io::IO, ::StackOverflowError) = print(io, "StackOverflowError:") showerror(io::IO, ::UndefRefError) = print(io, "UndefRefError: access to undefined reference") diff --git a/test/file.jl b/test/file.jl index 77e780a6314d8..ee7fb3295cc3b 100644 --- a/test/file.jl +++ b/test/file.jl @@ -13,6 +13,16 @@ subdir = joinpath(dir, "adir") mkdir(subdir) subdir2 = joinpath(dir, "adir2") mkdir(subdir2) +@test_throws SystemError mkdir(file) +let err = nothing + try + mkdir(file) + catch err + io = IOBuffer() + showerror(io, err) + @test takebuf_string(io) == "SystemError (with $file): mkdir: File exists" + end +end if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER) dirlink = joinpath(dir, "dirlink")