@@ -17,7 +17,8 @@ use adblock::engine::Engine as RustEngine;
1717use adblock:: lists:: FilterFormat ;
1818use adblock:: lists:: FilterSet as RustFilterSet ;
1919use pyo3:: class:: PyObjectProtocol ;
20- use pyo3:: exceptions:: PyValueError ;
20+ use pyo3:: create_exception;
21+ use pyo3:: exceptions:: PyException ;
2122use pyo3:: prelude:: * ;
2223use pyo3:: types:: PyBytes ;
2324use pyo3:: PyErr ;
@@ -31,12 +32,28 @@ use std::io::{Read, Write};
3132
3233/// Brave's adblocking library in Python!
3334#[ pymodule]
34- fn adblock ( _py : Python < ' _ > , m : & PyModule ) ->PyResult < ( ) > {
35+ fn adblock ( py : Python < ' _ > , m : & PyModule ) ->PyResult < ( ) > {
3536 m. add ( "__version__" , env ! ( "CARGO_PKG_VERSION" ) ) ?;
3637 m. add_class :: < Engine > ( ) ?;
3738 m. add_class :: < FilterSet > ( ) ?;
3839 m. add_class :: < BlockerResult > ( ) ?;
3940 m. add_class :: < UrlSpecificResources > ( ) ?;
41+ m. add ( "AdblockException" , py. get_type :: < AdblockException > ( ) ) ?;
42+ m. add ( "BlockerException" , py. get_type :: < BlockerException > ( ) ) ?;
43+ m. add ( "SerializationError" , py. get_type :: < SerializationError > ( ) ) ?;
44+ m. add (
45+ "DeserializationError" ,
46+ py. get_type :: < DeserializationError > ( ) ,
47+ ) ?;
48+ m. add (
49+ "OptimizedFilterExistence" ,
50+ py. get_type :: < OptimizedFilterExistence > ( ) ,
51+ ) ?;
52+ m. add (
53+ "BadFilterAddUnsupported" ,
54+ py. get_type :: < BadFilterAddUnsupported > ( ) ,
55+ ) ?;
56+ m. add ( "FilterExists" , py. get_type :: < FilterExists > ( ) ) ?;
4057Ok ( ( ) )
4158}
4259
@@ -141,9 +158,24 @@ impl Display for BlockerError {
141158}
142159}
143160
161+ create_exception ! ( adblock, AdblockException , PyException ) ;
162+ create_exception ! ( adblock, BlockerException , AdblockException ) ;
163+ create_exception ! ( adblock, SerializationError , BlockerException ) ;
164+ create_exception ! ( adblock, DeserializationError , BlockerException ) ;
165+ create_exception ! ( adblock, OptimizedFilterExistence , BlockerException ) ;
166+ create_exception ! ( adblock, BadFilterAddUnsupported , BlockerException ) ;
167+ create_exception ! ( adblock, FilterExists , BlockerException ) ;
168+
144169impl Into < PyErr > for BlockerError {
145170fn into ( self ) ->PyErr {
146- PyErr :: new :: < PyValueError , _ > ( format ! ( "{:?}" , self ) )
171+ let msg =format ! ( "{:?}" , self ) ;
172+ match self {
173+ Self :: SerializationError =>PyErr :: new :: < SerializationError , _ > ( msg) ,
174+ Self :: DeserializationError =>PyErr :: new :: < DeserializationError , _ > ( msg) ,
175+ Self :: OptimizedFilterExistence =>PyErr :: new :: < OptimizedFilterExistence , _ > ( msg) ,
176+ Self :: BadFilterAddUnsupported =>PyErr :: new :: < BadFilterAddUnsupported , _ > ( msg) ,
177+ Self :: FilterExists =>PyErr :: new :: < FilterExists , _ > ( msg) ,
178+ }
147179}
148180}
149181
@@ -163,7 +195,7 @@ fn filter_format_from_string(filter_format: &str) -> PyResult<FilterFormat> {
163195match filter_format{
164196"standard" =>Ok ( FilterFormat :: Standard ) ,
165197"hosts" =>Ok ( FilterFormat :: Hosts ) ,
166- _ =>Err ( PyErr :: new :: < PyValueError , _ > ( "Invalid format value" ) ) ,
198+ _ =>Err ( PyErr :: new :: < AdblockException , _ > ( "Invalid format value" ) ) ,
167199}
168200}
169201