Skip to content

Commit

Permalink
Merge pull request #190 from piotaixr/feature/use-correct-exception-l…
Browse files Browse the repository at this point in the history
…oad-schema-store

Use the specific exception type and its field to attempt loading a missing schema from file
  • Loading branch information
dasch authored Aug 29, 2023
2 parents 3b4b4e5 + b0ae475 commit eee6a91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion avro_turf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "avro", ">= 1.7.7", "< 1.12"
spec.add_dependency "avro", ">= 1.8.0", "< 1.12"
spec.add_dependency "excon", "~> 0.71"

spec.add_development_dependency "bundler", "~> 2.0"
Expand Down
36 changes: 15 additions & 21 deletions lib/avro_turf/schema_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,22 @@ def load_schema!(fullname, local_schemas_cache = {})
@schemas[fullname] = schema

schema
rescue ::Avro::SchemaParseError => e
# This is a hack in order to figure out exactly which type was missing. The
# Avro gem ought to provide this data directly.
if e.to_s =~ /"([\w\.]+)" is not a schema we know about/
# Try to first resolve a referenced schema from disk.
# If this is successful, the Avro gem will have mutated the
# local_schemas_cache, adding all the new schemas it found.
load_schema!($1, local_schemas_cache)

# Attempt to re-parse the original schema now that the dependency
# has been resolved and use the now-updated local_schemas_cache to
# pick up where we left off.
local_schemas_cache.delete(fullname)
# Ensure all sub-schemas are cleaned up to avoid conflicts when re-parsing
# schema.
local_schemas_cache.each do |schema_name, schema|
local_schemas_cache.delete(schema_name) unless File.exist?(build_schema_path(schema_name))
end
load_schema!(fullname, @schemas.dup)
else
raise
rescue ::Avro::UnknownSchemaError => e
# Try to first resolve a referenced schema from disk.
# If this is successful, the Avro gem will have mutated the
# local_schemas_cache, adding all the new schemas it found.
load_schema!(e.type_name, local_schemas_cache)

# Attempt to re-parse the original schema now that the dependency
# has been resolved and use the now-updated local_schemas_cache to
# pick up where we left off.
local_schemas_cache.delete(fullname)
# Ensure all sub-schemas are cleaned up to avoid conflicts when re-parsing
# schema.
local_schemas_cache.each_key do |schema_name|
local_schemas_cache.delete(schema_name) unless File.exist?(build_schema_path(schema_name))
end
load_schema!(fullname, @schemas.dup)
rescue Errno::ENOENT, Errno::ENAMETOOLONG
raise AvroTurf::SchemaNotFoundError, "could not find Avro schema at `#{schema_path}'"
end
Expand Down

0 comments on commit eee6a91

Please sign in to comment.