Skip to content

Commit

Permalink
refactor: refactor ACE-OAuth error handling
Browse files Browse the repository at this point in the history
Co-Authored-By: Falko <10247603+falko17@users.noreply.github.com>
  • Loading branch information
JKRhb and falko17 committed Aug 22, 2022
1 parent db7809d commit 0bd0b6b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
29 changes: 29 additions & 0 deletions lib/src/binding_coap/coap_binding_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import 'package:coap/coap.dart';

/// This [Exception] is thrown when an error within the CoAP Binding occurs.
class CoapBindingException implements Exception {
/// Constructor.
Expand All @@ -19,3 +21,30 @@ class CoapBindingException implements Exception {
return 'CoapBindingException: $_message';
}
}

/// Base class for [Exception]s that are thrown due to error responses.
abstract class CoapBindingResponseException extends CoapBindingException {
/// Constructor.
CoapBindingResponseException(CoapResponse response)
: super(
'${response.statusCodeString}. Payload: ${response.payloadString}',
);
}

/// [Exception] that is thrown if a client error occurs.
class CoapClientErrorException extends CoapBindingResponseException {
/// Constructor.
CoapClientErrorException(super.response);

@override
String toString() => 'CoapClientErrorException: $_message';
}

/// [Exception] that is thrown if a server error occurs.
class CoapServerErrorException extends CoapBindingResponseException {
/// Constructor.
CoapServerErrorException(super.response);

@override
String toString() => 'CoapServerErrorException: $_message';
}
10 changes: 2 additions & 8 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,8 @@ class CoapClient extends ProtocolClient {
return response;
}

final errorString = '${response.code}. Payload: ${response.payloadString}';

if (response.code.isServerError) {
throw CoapBindingException(
'Server error: $errorString',
);
throw CoapServerErrorException(response);
}

final aceCreationHint = response.creationHint;
Expand All @@ -341,9 +337,7 @@ class CoapClient extends ProtocolClient {
}
}

throw CoapBindingException(
'Client error: $errorString',
);
throw CoapClientErrorException(response);
}

@override
Expand Down

0 comments on commit 0bd0b6b

Please sign in to comment.