Flutter & Dart Error Handling Middleware Concept

Viewed 124

I check my errors between try blocks. How can I get rid of code repetition by creating an intermediate layer instead of rewriting it within each method?

As seen in the codes, I use try blocks in all operations.

Future<List<Post>> getAll(){
  try {
    //http get code
  }on SocketException {
    throw Failure('Message');
  }on HttpException {
    throw Failure("Http Exception Message");
  }on FormatException {
    throw Failure("Format Message");
  }
}

Future<Post> getPost(){
  try {
    //http get code
  }on SocketException {
    throw Failure('Message');
  }on HttpException {
    throw Failure("Http Exception Message");
  }on FormatException {
    throw Failure("Format Message");
  }
}
0 Answers
Related