Skip to content

Commit 3277987

Browse files
author
Alex Evanczuk
authored
Fix bad error messaging when JS package ownership fails (#17)
* Improve error messaging when JS package ownership fails * bump version * add error * fix tests
1 parent f274f7c commit 3277987

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_ownership (1.28.0)
4+
code_ownership (1.28.1)
55
code_teams (~> 1.0)
66
parse_packwerk
77
sorbet-runtime
@@ -15,7 +15,7 @@ GEM
1515
coderay (1.1.3)
1616
diff-lcs (1.4.4)
1717
method_source (1.0.0)
18-
parse_packwerk (0.10.1)
18+
parse_packwerk (0.11.0)
1919
sorbet-runtime
2020
parser (3.1.2.0)
2121
ast (~> 2.4.1)

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ File annotations are a last resort if there is no clear home for your code. File
3434
```ruby
3535
# @team MyTeam
3636
```
37+
38+
### Javascript Package Ownership
39+
Javascript package based ownership allows you to specify an owenrship key in a `package.json`. To use this, configure your `package.json` like this:
40+
41+
```json
42+
{
43+
// other keys
44+
"metadata": {
45+
"owner": "My Team"
46+
}
47+
// other keys
48+
}
49+
```
50+
51+
You can also tell `code_ownership` where to find JS packages in the configuration, like this:
52+
```yml
53+
js_package_paths:
54+
- frontend/javascripts/packages/*
55+
- frontend/other_location_for_packages/*
56+
```
57+
58+
This defaults `**/`, which makes it look for `package.json` files across your application.
59+
3760
## Usage: Reading CodeOwnership
3861
### `for_file`
3962
`CodeOwnership.for_file`, given a relative path to a file returns a `CodeTeams::Team` if there is a team that owns the file, `nil` otherwise.

code_ownership.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = "code_ownership"
3-
spec.version = '1.28.0'
3+
spec.version = '1.28.1'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'A gem to help engineering teams declare ownership of code'

lib/code_ownership/private/parse_js_packages.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ def self.from(pathname)
3232
name: package_name,
3333
metadata: package_loaded_json[METADATA] || {}
3434
)
35+
rescue JSON::ParserError => e
36+
error_message = <<~MESSAGE
37+
#{e.inspect}
38+
39+
#{pathname} has invalid JSON, so code ownership cannot be determined.
40+
41+
Please either make the JSON in that file valid or specify `js_package_paths` in config/code_ownership.yml.
42+
MESSAGE
43+
44+
raise InvalidCodeOwnershipConfigurationError.new(error_message)
3545
end
3646

3747
sig { returns(Pathname) }

spec/lib/code_ownership_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,29 @@
634634
end
635635
end
636636
end
637+
638+
context 'application has invalid JSON in package' do
639+
before do
640+
write_file('config/code_ownership.yml', {}.to_yaml)
641+
642+
write_file('frontend/javascripts/my_package/package.json', <<~CONTENTS)
643+
{ syntax error!!!
644+
"metadata": {
645+
"owner": "Foo"
646+
}
647+
}
648+
CONTENTS
649+
end
650+
651+
it 'lets the user know the their package JSON is invalid' do
652+
expect { CodeOwnership.validate! }.to raise_error do |e|
653+
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
654+
expect(e.message).to match /JSON::ParserError.*?unexpected token/
655+
expect(e.message).to include 'frontend/javascripts/my_package/package.json has invalid JSON, so code ownership cannot be determined.'
656+
expect(e.message).to include 'Please either make the JSON in that file valid or specify `js_package_paths` in config/code_ownership.yml.'
657+
end
658+
end
659+
end
637660
end
638661

639662
describe '.for_file' do

0 commit comments

Comments
 (0)