Skip to content

Commit

Permalink
Add sigs for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Sep 27, 2023
1 parent f133a59 commit 7b4b63c
Showing 1 changed file with 83 additions and 13 deletions.
96 changes: 83 additions & 13 deletions common/lib/dependabot/errors.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# typed: true
# typed: strong
# frozen_string_literal: true

require "sorbet-runtime"
require "dependabot/utils"

module Dependabot
class DependabotError < StandardError
extend T::Sig

BASIC_AUTH_REGEX = %r{://(?<auth>[^:]*:[^@%\s]+(@|%40))}
# Remove any path segment from fury.io sources
FURY_IO_PATH_REGEX = %r{fury\.io/(?<path>.+)}

sig { params(message: T.nilable(String)).void }
def initialize(message = nil)
super(sanitize_message(message))
end

private

sig { params(message: T.nilable(String)).returns(T.nilable(String)) }
def sanitize_message(message)
return message unless message.is_a?(String)

Expand All @@ -26,18 +31,25 @@ def sanitize_message(message)
filter_sensitive_data(message)
end

sig { params(message: String).returns(String) }
def filter_sensitive_data(message)
replace_capture_groups(message, BASIC_AUTH_REGEX, "")
end

sig { params(source: String).returns(String) }
def sanitize_source(source)
source = filter_sensitive_data(source)
replace_capture_groups(source, FURY_IO_PATH_REGEX, "<redacted>")
end

sig do
params(
string: String,
regex: Regexp,
replacement: String
).returns(String)
end
def replace_capture_groups(string, regex, replacement)
return string unless string.is_a?(String)

string.scan(regex).flatten.compact.reduce(string) do |original_msg, match|
original_msg.gsub(match, replacement)
end
Expand All @@ -55,17 +67,25 @@ class NotImplemented < DependabotError; end
#####################

class BranchNotFound < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :branch_name

sig { params(branch_name: String, msg: T.nilable(String)).void }
def initialize(branch_name, msg = nil)
@branch_name = branch_name
super(msg)
end
end

class RepoNotFound < DependabotError
extend T::Sig

sig { returns(Dependabot::Source) }
attr_reader :source

sig { params(source: Dependabot::Source, msg: T.nilable(String)).void }
def initialize(source, msg = nil)
@source = source
super(msg)
Expand All @@ -77,38 +97,50 @@ def initialize(source, msg = nil)
#####################

class DependencyFileNotFound < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :file_path

sig { params(file_path: String, msg: T.nilable(String)).void }
def initialize(file_path, msg = nil)
@file_path = file_path
super(msg || "#{file_path} not found")
end

sig { returns(String) }
def file_name
file_path.split("/").last
T.must(file_path.split("/").last)
end

sig { returns(String) }
def directory
# Directory should always start with a `/`
file_path.split("/")[0..-2].join("/").sub(%r{^/*}, "/")
T.must(file_path.split("/")[0..-2]).join("/").sub(%r{^/*}, "/")
end
end

class DependencyFileNotParseable < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :file_path

sig { params(file_path: String, msg: T.nilable(String)).void }
def initialize(file_path, msg = nil)
@file_path = file_path
super(msg || "#{file_path} not parseable")
end

sig { returns(String) }
def file_name
file_path.split("/").last
T.must(file_path.split("/").last)
end

sig { returns(String) }
def directory
# Directory should always start with a `/`
file_path.split("/")[0..-2].join("/").sub(%r{^/*}, "/")
T.must(file_path.split("/")[0..-2]).join("/").sub(%r{^/*}, "/")
end
end

Expand All @@ -121,10 +153,14 @@ class DependencyFileNotResolvable < DependabotError; end
#######################

class PrivateSourceAuthenticationFailure < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :source

sig { params(source: String).void }
def initialize(source)
@source = sanitize_source(source)
@source = T.let(sanitize_source(source), String)
msg = "The following source could not be reached as it requires " \
"authentication (and any provided details were invalid or lacked " \
"the required permissions): #{@source}"
Expand All @@ -133,26 +169,38 @@ def initialize(source)
end

class PrivateSourceTimedOut < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :source

sig { params(source: String).void }
def initialize(source)
@source = sanitize_source(source)
@source = T.let(sanitize_source(source), String)
super("The following source timed out: #{@source}")
end
end

class PrivateSourceCertificateFailure < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :source

sig { params(source: String).void }
def initialize(source)
@source = sanitize_source(source)
@source = T.let(sanitize_source(source), String)
super("Could not verify the SSL certificate for #{@source}")
end
end

class MissingEnvironmentVariable < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :environment_variable

sig { params(environment_variable: String).void }
def initialize(environment_variable)
@environment_variable = environment_variable
super("Missing environment variable #{@environment_variable}")
Expand All @@ -168,11 +216,15 @@ class InconsistentRegistryResponse < DependabotError; end
###########################

class GitDependenciesNotReachable < DependabotError
extend T::Sig

sig { returns(T::Array[String]) }
attr_reader :dependency_urls

sig { params(dependency_urls: T::Array[String]).void }
def initialize(*dependency_urls)
@dependency_urls =
dependency_urls.flatten.map { |uri| filter_sensitive_data(uri) }
T.let(dependency_urls.flatten.map { |uri| filter_sensitive_data(uri) }, T::Array[String])

msg = "The following git URLs could not be retrieved: " \
"#{@dependency_urls.join(', ')}"
Expand All @@ -181,8 +233,12 @@ def initialize(*dependency_urls)
end

class GitDependencyReferenceNotFound < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :dependency

sig { params(dependency: String).void }
def initialize(dependency)
@dependency = dependency

Expand All @@ -193,19 +249,33 @@ def initialize(dependency)
end

class PathDependenciesNotReachable < DependabotError
extend T::Sig

sig { returns(T::Array[String]) }
attr_reader :dependencies

sig { params(dependencies: T::Array[T.untyped]).void }
def initialize(*dependencies)
@dependencies = dependencies.flatten
@dependencies = T.let(dependencies.flatten, T::Array[String])
msg = "The following path based dependencies could not be retrieved: " \
"#{@dependencies.join(', ')}"
super(msg)
end
end

class GoModulePathMismatch < DependabotError
attr_reader :go_mod, :declared_path, :discovered_path
extend T::Sig

sig { returns(String) }
attr_reader :go_mod

sig { returns(String) }
attr_reader :declared_path

sig { returns(String) }
attr_reader :discovered_path

sig { params(go_mod: String, declared_path: String, discovered_path: String).void }
def initialize(go_mod, declared_path, discovered_path)
@go_mod = go_mod
@declared_path = declared_path
Expand Down

0 comments on commit 7b4b63c

Please sign in to comment.