Skip to content

Commit

Permalink
mkdir: supply path to SystemError
Browse files Browse the repository at this point in the history
Some mkdir errors are very hard to debug unless you know what path it was trying to create.
  • Loading branch information
timholy committed Nov 16, 2015
1 parent aa24ee0 commit 30c465f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##

Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ subdir = joinpath(dir, "adir")
mkdir(subdir)
subdir2 = joinpath(dir, "adir2")
mkdir(subdir2)
@test_throws SystemError mkdir(file)
try
mkdir(file)
catch err
io = IOBuffer()
showerror(io, err)
@test takebuf_string(io) == "SystemError (with $file): mkdir: File exists"
end

if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
dirlink = joinpath(dir, "dirlink")
Expand Down

0 comments on commit 30c465f

Please sign in to comment.