@@ -12,17 +12,18 @@ final pageProvider = Provider((ref) => PageProvider(ref));
1212
1313class FetchError implements Exception {
1414 final String message;
15+ final String path;
1516 final int code;
1617 final String body;
1718 final String ? url;
1819
19- FetchError (this .message, http.Response ? response)
20+ FetchError (this .message, this .path, [ http.Response ? response] )
2021 : code = response? .statusCode ?? 0 ,
2122 body = response? .body ?? '' ,
2223 url = response? .request? .url.toString ();
2324
2425 @override
25- String toString () => '$message : $code $body ' ;
26+ String toString () => '$message for $ path : $code $body ' ;
2627}
2728
2829class PageProvider {
@@ -40,7 +41,7 @@ class PageProvider {
4041 final url = Uri .https (kBaseUrl);
4142 final response = await http.get (url);
4243 if (response.statusCode != 200 ) {
43- throw FetchError ('Failed to get a cookie' , response);
44+ throw FetchError ('Failed to get a cookie' , '/' , response);
4445 }
4546 final data =
4647 response.headers['set-cookie' ] ?? response.headers['Set-Cookie' ];
@@ -68,6 +69,15 @@ class PageProvider {
6869 );
6970 }
7071
72+ Future forgetPage (String path) async {
73+ final db = await _ref.read (databaseProvider).database;
74+ await db.delete (
75+ CachedPage .kTableName,
76+ where: 'url = ?' ,
77+ whereArgs: [path],
78+ );
79+ }
80+
7181 Future <String > fetchPage (String path) async {
7282 if (_cookie == null ) await _updateCookie ();
7383
@@ -92,13 +102,16 @@ class PageProvider {
92102
93103 if (response.statusCode != 200 ) {
94104 if (cached != null ) return cached.content;
95- throw FetchError ('Error on $ path ' , response);
105+ throw FetchError ('Error' , path, response);
96106 }
97107
98108 body = utf8.decode (response.bodyBytes);
99109 } on TimeoutException {
100110 if (cached != null ) return cached.content;
101- throw FetchError ('Timeout for $path ' , null );
111+ throw FetchError ('Timeout' , path);
112+ }
113+ if (body.length < 2 ) {
114+ throw FetchError ('Empty page' , path);
102115 }
103116
104117 _saveToCache (path, body);
0 commit comments