-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from rollbar/robust_http_error_handling
More robust HTTP error handling
- Loading branch information
Showing
23 changed files
with
864 additions
and
252 deletions.
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
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 |
---|---|---|
@@ -1,7 +1,30 @@ | ||
import 'package:http/http.dart' as http; | ||
|
||
enum HttpMethod { get, head, post, put, delete, connect, options, trace, patch } | ||
|
||
enum HttpStatus { info, success, redirect, clientError, serverError } | ||
|
||
typedef HttpHeaders = Map<String, String>; | ||
|
||
extension HttpMethodName on HttpMethod { | ||
String get name => toString().split('.').last.toUpperCase(); | ||
} | ||
|
||
extension HttpResponseExtension on http.Response { | ||
/// The HTTP status for this response derived from the status code. | ||
HttpStatus get status { | ||
if (statusCode >= 100 && statusCode < 200) { | ||
return HttpStatus.info; | ||
} else if (statusCode >= 200 && statusCode < 300) { | ||
return HttpStatus.success; | ||
} else if (statusCode >= 300 && statusCode < 400) { | ||
return HttpStatus.redirect; | ||
} else if (statusCode >= 400 && statusCode < 500) { | ||
return HttpStatus.clientError; | ||
} else if (statusCode >= 500 && statusCode < 600) { | ||
return HttpStatus.serverError; | ||
} else { | ||
throw StateError('http status code $statusCode is invalid.'); | ||
} | ||
} | ||
} |
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,51 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
abstract class Result<T, E extends Error /*| Exception*/ > { | ||
bool get isSuccess => this is Success<T, E>; | ||
bool get isFailure => this is Failure<T, E>; | ||
|
||
T get success => (this as Success<T, E>).value; | ||
E get failure => (this as Failure<T, E>).error; | ||
|
||
Result<U, E> map<U>(U Function(T) transform) => isSuccess | ||
? Success(transform((this as Success<T, E>).value)) | ||
: Failure((this as Failure<T, E>).error); | ||
} | ||
|
||
@sealed | ||
@immutable | ||
class Success<T, E extends Error> extends Result<T, E> { | ||
final T value; | ||
|
||
Success(this.value); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
(other is Success<T, E> && other.value == value); | ||
|
||
@override | ||
int get hashCode => value.hashCode; | ||
|
||
@override | ||
String toString() => 'Result.Success($value)'; | ||
} | ||
|
||
@sealed | ||
@immutable | ||
class Failure<T, E extends Error> extends Result<T, E> { | ||
final E error; | ||
|
||
Failure(this.error); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
(other is Failure<T, E> && other.error == error); | ||
|
||
@override | ||
int get hashCode => error.hashCode; | ||
|
||
@override | ||
String toString() => 'Result.Failure($error)'; | ||
} |
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
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
Oops, something went wrong.