Skip to content

Commit

Permalink
fix: throw exception when an item isn't found by searchByUniqueName
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerOrnstein committed Sep 13, 2023
1 parent ed9dddd commit 7636195
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/src/clients/items_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';

import 'package:warframestat_client/warframestat_client.dart';

Expand Down Expand Up @@ -122,14 +123,20 @@ class WarframeItemsClient extends WarframestatClient {
}

/// Pulls an item useing it's uniqueName.
///
/// Returns null when an item with the uniqueName doesn't exist.
Future<Item> searchByUniqueName(String uniqueName) async {
final encodedUniqueName = Uri.encodeQueryComponent(uniqueName);
final item = await _get<Map<String, dynamic>>(
final request = await _get<Map<String, dynamic>>(
'/$encodedUniqueName/',
query: {'by': 'uniqueName'},
);

return toItem(item);
if (request['code'] == HttpStatus.notFound) {
throw ItemNotFound(uniqueName);
}

return toItem(request);
}

Future<T> _get<T>(String path, {Map<String, dynamic>? query}) async {
Expand Down
13 changes: 13 additions & 0 deletions lib/src/utils/exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// {@template item_not_found}
/// Item exceptions thrown when an item was not found.
/// {@endtemplate}
class ItemNotFound implements Exception {
/// {@macro item_not_found}
const ItemNotFound(this.item);

/// Item name or uniqueName.
final String item;

@override
String toString() => '$item was not found.';
}
1 change: 1 addition & 0 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'exceptions.dart';
export 'item_categories.dart';
export 'items_utils.dart';

0 comments on commit 7636195

Please sign in to comment.