-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Violation: | ||
Use '.exception' over '.error' inside except blocks | ||
""" | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def bad(): | ||
try: | ||
a = 1 | ||
except Exception: | ||
logger.error("Context message here") | ||
|
||
def good(): | ||
try: | ||
a = 1 | ||
except Exception: | ||
logger.exception("Context message here") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1781,6 +1781,9 @@ | |
"TRY30", | ||
"TRY300", | ||
"TRY301", | ||
"TRY4", | ||
"TRY40", | ||
"TRY400", | ||
"TYP", | ||
"TYP0", | ||
"TYP00", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Excepthandler, ExcepthandlerKind, ExprKind, Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct ErrorInsteadException; | ||
); | ||
|
||
impl Violation for ErrorInsteadException { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use logging '.exception' instead of '.error'") | ||
} | ||
} | ||
fn visit_stmt(stmt: &Stmt) -> bool { | ||
if let StmtKind::Expr { value, .. } = &stmt.node { | ||
if let ExprKind::Call { func, .. } = &value.node { | ||
if let ExprKind::Attribute { attr, .. } = &func.node { | ||
return attr == &"error".to_string(); | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
/// TRY400 | ||
pub fn error_instead_exception(checker: &mut Checker, handlers: &[Excepthandler]) { | ||
for handler in handlers { | ||
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node; | ||
for stmt in body { | ||
if visit_stmt(stmt) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
ErrorInsteadException, | ||
Range::from_located(stmt), | ||
)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters