Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make errors into idiomatic exceptions #77

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/cas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class Cas {
} else {
// exec completes only with valid response
_completer_bool.completeError(
RedisError("exec response is not expected, but is $resp"));
RedisException("exec response is not expected, but is $resp"));
}
}).catchError((e) {
// dont do anything
_completer_bool.complete(true); // retry
}, test: (e) => e is TransactionError);
}, test: (e) => e is TransactionException);
});
}
}
9 changes: 4 additions & 5 deletions lib/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ class Command {
this.serializer = other.serializer;
}

Command setParser(Parser p){
Command setParser(Parser p) {
this.parser = p;
return this;
}

Command setSerializer(Serializer s){
Command setSerializer(Serializer s) {
this.serializer = s;
return this;
}


/// Serialize and send data to server
///
Expand All @@ -44,9 +43,9 @@ class Command {
/// send_object(["SET","key","value"]);
Future send_object(Object obj) {
try {
return _connection._sendraw(parser,serializer.serialize(obj)).then((v) {
return _connection._sendraw(parser, serializer.serialize(obj)).then((v) {
// turn RedisError into exception
if (v is RedisError) {
if (v is RedisException) {
return Future.error(v);
} else {
return v;
Expand Down
68 changes: 57 additions & 11 deletions lib/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,73 @@

part of redis;


// this class is returned when redis response is type error
class RedisError {
@Deprecated('Use RedisException instead')
class RedisError {
String e;

RedisError(this.e);
String toString() { return "RedisError($e)";}

String get error => e;

String toString() {
return "RedisError($e)";
}
}

// thiss class is returned when parsing in client side (aka this libraray)
// get error
class RedisRuntimeError {
/// This class is returned when redis response is type error
// ignore: deprecated_member_use_from_same_package
class RedisException extends RedisError implements Exception {
RedisException(String message) : super(message);

String toString() {
return "RedisException($e)";
}
}

@Deprecated('Use RedisRuntimeException instead')
class RedisRuntimeError {
String e;

RedisRuntimeError(this.e);
String toString() { return "RedisRuntimeError($e)";}

String get error => e;

String toString() {
return "RedisRuntimeError($e)";
}
}

/// This class is returned when parsing in client side (aka this libraray)
// ignore: deprecated_member_use_from_same_package
class RedisRuntimeException extends RedisRuntimeError implements Exception {
RedisRuntimeException(String message) : super(message);

String toString() {
return "RedisRuntimeException($e)";
}
}

class TransactionError {
@Deprecated('Use TransactionException instead')
class TransactionError {
String e;

TransactionError(this.e);
String toString() { return "TranscationError($e)";}
String get error => e;

String get error => e;

String toString() {
return "TransactionError($e)";
}
}

/// This class is returned when transaction fails
// ignore: deprecated_member_use_from_same_package
class TransactionException extends TransactionError implements Exception {
TransactionException(String message) : super(message);

String get error => e;

String toString() {
return "TransactionException($e)";
}
}
37 changes: 18 additions & 19 deletions lib/redisparser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

part of redis;

class RedisParser extends Parser {

}
class RedisParser extends Parser {}

class RedisParserBulkBinary extends Parser {
Future parseBulk(LazyStream s) {
Expand All @@ -21,17 +19,17 @@ class RedisParserBulkBinary extends Parser {
return null;
if (i >= 0) {
//i of bulk data
return s.take_n(i).then((lst) => takeCRLF(
s, lst)); //consume CRLF and return list
return s
.take_n(i)
.then((lst) => takeCRLF(s, lst)); //consume CRLF and return list
} else {
return Future.error(
RedisRuntimeError("cant process buld data less than -1"));
RedisRuntimeException("cant process buld data less than -1"));
}
});
}
}
}


class Parser {
static final UTF8 = const Utf8Codec();
static const int CR = 13;
Expand All @@ -53,7 +51,8 @@ class Parser {
//now check for LF
return s.take_n(1).then((lf) {
if (lf[0] != LF) {
return Future.error(RedisRuntimeError("received element is not LF"));
return Future.error(
RedisRuntimeException("received element is not LF"));
}
return list;
});
Expand All @@ -67,15 +66,15 @@ class Parser {
if (data[0] == CR && data[1] == LF) {
return r;
} else {
return Future.error(RedisRuntimeError("expeting CRLF"));
return Future.error(RedisRuntimeException("expeting CRLF"));
}
});
}

Future parse(LazyStream s){
Future parse(LazyStream s) {
return parseredisresponse(s);
}

Future parseredisresponse(LazyStream s) {
return s.take_n(1).then((list) {
int cmd = list[0];
Expand All @@ -92,7 +91,7 @@ class Parser {
return parseError(s);
default:
return Future.error(
RedisRuntimeError("got element that cant not be parsed"));
RedisRuntimeException("got element that cant not be parsed"));
}
});
}
Expand All @@ -103,8 +102,8 @@ class Parser {
});
}

Future<RedisError> parseError(LazyStream s) {
return parseSimpleString(s).then((str) => RedisError(str));
Future<RedisException> parseError(LazyStream s) {
return parseSimpleString(s).then((str) => RedisException(str));
}

Future<int> parseInt(LazyStream s) {
Expand All @@ -122,7 +121,7 @@ class Parser {
s, UTF8.decode(lst))); //consume CRLF and return decoded list
} else {
return Future.error(
RedisRuntimeError("cant process buld data less than -1"));
RedisRuntimeException("cant process buld data less than -1"));
}
});
}
Expand Down Expand Up @@ -153,7 +152,7 @@ class Parser {
return consumeList(s, i, a);
} else {
return Future.error(
RedisRuntimeError("cant process array data less than -1"));
RedisRuntimeException("cant process array data less than -1"));
}
});
}
Expand All @@ -163,13 +162,13 @@ class Parser {
int sign = 1;
var v = arr.fold(0, (dynamic a, b) {
if (b == 45) {
if (a != 0) throw RedisRuntimeError("cannot parse int");
if (a != 0) throw RedisRuntimeException("cannot parse int");
sign = -1;
return 0;
} else if ((b >= 48) && (b < 58)) {
return a * 10 + b - 48;
} else {
throw RedisRuntimeError("cannot parse int");
throw RedisRuntimeException("cannot parse int");
}
});
return v * sign;
Expand Down
17 changes: 10 additions & 7 deletions lib/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
part of redis;

class _WarningConnection {
noSuchMethod(_) => throw RedisRuntimeError("Transaction in progress. "
noSuchMethod(_) => throw RedisRuntimeException("Transaction in progress. "
"Please complete Transaction with .exec");
}

Expand All @@ -28,17 +28,20 @@ class Transaction extends Command {

Future send_object(object) {
if (transaction_completed) {
return Future.error(RedisRuntimeError("Transaction already completed."));
return Future.error(
RedisRuntimeException("Transaction already completed."));
}

Completer c = Completer();
_queue.add(c);
super.send_object(object).then((msg) {
if (msg.toString().toLowerCase() != "queued") {
c.completeError(
RedisError("Could not enqueue command: " + msg.toString()));
RedisException("Could not enqueue command: " + msg.toString()));
}
}).catchError((error){ c.completeError(error); });
}).catchError((error) {
c.completeError(error);
});
return c.future;
}

Expand All @@ -57,14 +60,14 @@ class Transaction extends Command {
while (_queue.isNotEmpty) {
_queue.removeFirst();
}
// return Future.error(TransactionError("transaction error "));
throw TransactionError("transaction error ");
// return Future.error(TransactionException("transaction error "));
throw TransactionException("transaction error ");
//return null;
} else {
if (list.length != _queue.length) {
int? diff = list.length - _queue.length;
//return
throw RedisRuntimeError(
throw RedisRuntimeException(
"There was $diff command(s) executed during transcation,"
"not going trough Transation handler");
}
Expand Down
2 changes: 1 addition & 1 deletion test/close_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ main() {
});
}

const Matcher isRedisError = TypeMatcher<RedisError>();
const Matcher isRedisError = TypeMatcher<RedisException>();
2 changes: 1 addition & 1 deletion test/error_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ main() {
});
}

const Matcher isRedisError = TypeMatcher<RedisError>();
const Matcher isRedisError = TypeMatcher<RedisException>();
6 changes: 3 additions & 3 deletions test/transactions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
"Transaction value should not be interfered by actions outside of transaction");
}).catchError((e) {
print("got test error $e");
expect(e, TypeMatcher<TransactionError>());
expect(e, TypeMatcher<TransactionException>());
});

// Increase value out of transaction
Expand All @@ -39,7 +39,7 @@ void main() {

//Test using command fail during transaction
expect(() => cmd1.send_object(['SET', key, 0]),
throwsA(TypeMatcher<RedisRuntimeError>()),
throwsA(TypeMatcher<RedisRuntimeException>()),
reason: "Command should not be usable during transaction");

expect(trans.exec(), completion(equals("OK")),
Expand All @@ -49,7 +49,7 @@ void main() {
reason: "Value should be final value $n after transaction complete");

expect(() => trans.send_object(["GET", key]),
throwsA(TypeMatcher<RedisRuntimeError>()),
throwsA(TypeMatcher<RedisRuntimeException>()),
reason:
"Transaction object should not be usable after finishing transaction");
});
Expand Down