Skip to content

Commit

Permalink
Merge pull request #26 from namib-project/coap-exceptions
Browse files Browse the repository at this point in the history
fix(binding-coap): improve reponse error handling
  • Loading branch information
JKRhb authored Aug 22, 2022
2 parents db7809d + a2e0654 commit 33a255e
Show file tree
Hide file tree
Showing 3 changed files with 44 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';
}
11 changes: 3 additions & 8 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class CoapClient extends ProtocolClient {
}

coapClient.close();
response.checkResponseCode();
return response.content;
}

Expand Down Expand Up @@ -316,12 +317,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 +338,7 @@ class CoapClient extends ProtocolClient {
}
}

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

@override
Expand Down
12 changes: 12 additions & 0 deletions lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ extension ResponseExtension on CoapResponse {
return Content(_contentType, _payloadStream);
}

/// Checks the [code] of this [CoapResponse] and throws an [Exception] if it
/// should indicate an error.
void checkResponseCode() {
if (code.isServerError) {
throw CoapServerErrorException(this);
}

if (code.isErrorResponse) {
throw CoapClientErrorException(this);
}
}

/// Validates the payload and returns a serialized ACE creation hint if
/// successful.
AuthServerRequestCreationHint? get creationHint {
Expand Down

0 comments on commit 33a255e

Please sign in to comment.