Skip to content

libgit2 can't handle some large error codes #56763

Open
@inkydragon

Description

Sometimes libgit2 functions return large negative numbers, representing non-libgit2 errors (usually network errors), but the current Enum type (LibGit2.Error.Code) cannot contain them.

TIMEOUT = Cint(-37)) # The operation timed out

struct GitError <: Exception
class::Class
code::Code
msg::String
end

GitError(err_code::Integer) = GitError(Code(err_code))
function GitError(err_code::Code)
err_class, err_msg = last_error()
return GitError(err_class, err_code, err_msg)
end

An ArgumentError: invalid value for Enum Code is thrown when the error code abs(err_code) > 40.
An example: https://buildkite.com/julialang/julia-master/builds/42812#01939979-4dae-44ed-9e39-1f693a963576/863-969

Pkg             (2) |         failed at 2024-12-06T01:22:16.609
Error During Test at /cache/build/tester-demeter6-11/julialang/julia-master/julia-8c760fdc68/share/julia/stdlib/v1.12/Pkg/test/sources.jl:8
  Got exception outside of a @test
  ArgumentError: invalid value for Enum Code: -30848
  Stacktrace:
    [1] enum_argument_error(typename::Symbol, x::Int32)
      @ Base.Enums ./Enums.jl:93
    [2] Code
      @ ./Enums.jl:212 [inlined]
    [3] LibGit2.Error.GitError(err_code::Int32)
      @ LibGit2.Error /cache/build/tester-demeter6-11/julialang/julia-master/julia-8c760fdc68/share/julia/stdlib/v1.12/LibGit2/src/error.jl:103
    [4] macro expansion
      @ /cache/build/tester-demeter6-11/julialang/julia-master/julia-8c760fdc68/share/julia/stdlib/v1.12/LibGit2/src/error.jl:115 [inlined]
    [5] clone(repo_url::SubString{String}, repo_path::String, clone_opts::LibGit2.CloneOptions)
      @ LibGit2 /cache/build/tester-demeter6-11/julialang/julia-master/julia-8c760fdc68/share/julia/stdlib/v1.12/LibGit2/src/repository.jl:470
    [6] clone(repo_url::SubString{String}, repo_path::String; branch::String, isbare::Bool, remote_cb::Ptr{Nothing}, credentials::LibGit2.CachedCredentials, callbacks::Dict{Symbol, Tuple{Ptr{Nothing}, Any}})
      @ LibGit2 /cache/build/tester-demeter6-11/julialang/julia-master/julia-8c760fdc68/share/julia/stdlib/v1.12/LibGit2/src/LibGit2.jl:584
    [7] clone
      @ /cache/build/tester-demeter6-11/julialang/julia-master/julia-8c760fdc68/share/julia/stdlib/v1.12/LibGit2/src/LibGit2.jl:557 [inlined]

Perhaps the type restriction on GitError.code could be relaxed to cover these errors.

 struct GitError <: Exception 
     class::Class 
-    code::Code 
+    code::Union{Code, Int32}
     msg::String 
 end 

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    error handlingHandling of exceptions by Julia or the userlibgit2The libgit2 library or the LibGit2 stdlib module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions