Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mhernand40
Copy link
Contributor

An Exceptions rule that reports when an object extend any type of Throwable. Although using object may look innocent at times, having one that extends a Throwable/Exception inadvertently introduces a global singleton exception instance. Any custom exception should be made instantiable by being defined as a regular class.

Addresses #3436.

@codecov
Copy link

codecov bot commented Feb 4, 2021

Codecov Report

Merging #3443 (c843e63) into master (fa79c99) will increase coverage by 0.02%.
The diff coverage is 85.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3443      +/-   ##
============================================
+ Coverage     80.26%   80.28%   +0.02%     
- Complexity     2778     2785       +7     
============================================
  Files           453      454       +1     
  Lines          8391     8415      +24     
  Branches       1605     1609       +4     
============================================
+ Hits           6735     6756      +21     
  Misses          787      787              
- Partials        869      872       +3     
Impacted Files Coverage Δ Complexity Δ
.../detekt/rules/exceptions/ObjectExtendsThrowable.kt 83.33% <83.33%> (ø) 7.00 <7.00> (?)
...osch/detekt/rules/exceptions/ExceptionsProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)
...gitlab/arturbosch/detekt/cli/runners/AstPrinter.kt 100.00% <0.00%> (ø) 4.00% <0.00%> (ø%)
...ain/kotlin/io/github/detekt/metrics/LinesOfCode.kt 93.10% <0.00%> (+0.79%) 0.00% <0.00%> (ø%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa79c99...b6197eb. Read the comment docs.

CodeSmell(
issue = issue,
entity = Entity.from(element = it),
message = issue.description
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using a simpler message with the class name. Duplicating issue description into message makes the output feel duplicate. (See https://github.com/chao2zhang/detekt/pull/5/files, if both the message and issue description are duplicate and length, it will look pretty bad)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I was trying to maintain consistency with some of the existing exception rules, such as:

These are just a few that reference issue.description when setting the message parameter.

Are we satisfied with the current issue description or do we feel that even the issue description could use some shortening? If we make it shorter, do we still want a separate CodeSmell message? Thanks again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not forgotten about this. Just awaiting further feedback. 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree with chao here. One is the desciprtion of the rule and the oher is the description of one particular smell. I think that a description like: "XXX should be a class instead of an object because it extends from YYY" is enought for the message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or "from a throwable class"... or something like this. I'm not too good with this things :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks everyone. Let me know if this works b6197eb.

Comment on lines 20 to 24
object BanException : Throwable()
object AuthException : RuntimeException()
object ReportedException : Exception()
object FatalException : Error()
object ObjectCustomException : CustomException("singleton custom exception")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the exhaustive test cases!

Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👍 Left a couple of nits

@BraisGabin BraisGabin merged commit fab88df into detekt:master Feb 5, 2021
@mhernand40 mhernand40 deleted the new-rule-object-extends-throwable branch February 5, 2021 18:52
@cortinico cortinico added this to the 1.16.0 milestone Feb 19, 2021
This was referenced Mar 11, 2021
This was referenced Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants