Skip to content

Commit

Permalink
feature: Improve error handling (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha authored Feb 13, 2023
1 parent 9429444 commit 523e0b0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.2.0

- Update nyxx to 4.3.0

## 3.1.0

- Fixup tag deleting (invalid column in db)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.9'
services:
running_on_dart:
image: ghcr.io/nyxx-discord/running_on_dart:3.1.0
image: ghcr.io/nyxx-discord/running_on_dart:3.2.0
container_name: running_on_dart
env_file:
- .env
Expand Down
1 change: 1 addition & 0 deletions lib/running_on_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export 'src/services/prometheus.dart';
export 'src/services/reminder.dart';
export 'src/services/tag.dart';
export 'src/settings.dart';
export 'src/exception.dart';
13 changes: 9 additions & 4 deletions lib/src/commands/admin.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:running_on_dart/src/checks.dart';
import 'package:running_on_dart/src/exception.dart';

ChatGroup admin = ChatGroup(
'admin',
Expand Down Expand Up @@ -40,10 +41,14 @@ ChatGroup admin = ChatGroup(
toRemove = channelMessages.where((message) => message.author.id == user.id);
}

if (toRemove.length == 1) {
await toRemove.first.delete();
} else {
await context.channel.bulkRemoveMessages(toRemove);
try {
if (toRemove.length == 1) {
await toRemove.first.delete();
} else {
await context.channel.bulkRemoveMessages(toRemove);
}
} on IHttpResponseError catch (e) {
throw CheckedBotException(e.message);
}

count -= toRemove.length;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/commands/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ChatCommand info = ChatCommand(
})
..addFooter((footer) {
footer.text = 'nyxx ${Constants.version}'
' | $version'
' | ROD v$version'
' | Shard ${(context.guild?.shard.id ?? 0) + 1} of ${(context.client as INyxxWebsocket).shards}'
' | Dart SDK ${Platform.version.split('(').first}';
})
Expand Down
11 changes: 10 additions & 1 deletion lib/src/error_handler.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:html';

import 'package:duration/duration.dart';
import 'package:logging/logging.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:running_on_dart/src/exception.dart';

final _logger = Logger('ROD.CommandErrors');

Expand Down Expand Up @@ -53,7 +56,13 @@ void commandErrorHandler(CommandsException error) async {
description = "Your command couldn't be executed because we were unable to understand your input."
" Please try again with different inputs or contact a developer for more information.";
} else if (error is UncaughtException) {
_logger.severe('Uncaught exception in command: ${error.exception}');
final exception = error.exception;
if (exception is CheckedBotException) {
title = "Cannot process invoked command";
description = exception.message;
}

_logger.severe('Uncaught exception in command: $exception)');
}

// Send a generic "an error occurred" response
Expand Down
8 changes: 8 additions & 0 deletions lib/src/exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CheckedBotException implements Exception {
final String message;

CheckedBotException(this.message);

@override
String toString() => "CheckedBotException: $message";
}
2 changes: 1 addition & 1 deletion lib/src/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:io';

import 'package:nyxx/nyxx.dart';

String get version => '3.1.0';
String get version => '3.2.0';

/// Get a [String] from an environment variable, throwing an exception if it is not set.
///
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: running_on_dart
version: 3.1.0
version: 3.2.0
description: Discord Bot for nyxx development
homepage: https://github.com/nyxx-discord/running_on_dart
repository: https://github.com/nyxx-discord/running_on_dart
Expand All @@ -20,7 +20,7 @@ dependencies:
logging: ^1.0.2
migent:
git: https://github.com/nyxx-discord/migent.git
nyxx: ^3.4.1
nyxx: ^4.3.0
nyxx_commands: ^4.2.0
nyxx_interactions: ^4.1.0
nyxx_pagination: ^2.2.0
Expand Down

0 comments on commit 523e0b0

Please sign in to comment.