Skip to content

Commit

Permalink
Merge pull request #2 from obot-ai/AddException
Browse files Browse the repository at this point in the history
Fetcherが想定外のレスポンスを取得した場合に例外を発生させるように改修
  • Loading branch information
LINYE-MARIANA authored Apr 24, 2024
2 parents 2d21faf + f40436e commit 8fa7e4c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
21 changes: 15 additions & 6 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ void main(List<String> args) async {
}
}

print("Fetching [$locale] data from $host with API key $apiKey");
Generator generator = Generator();

print("Fetching [$locale] data from $host with API key $apiKey");
Fetcher fetcher = Fetcher(
apiKey: apiKey,
getEndpoint: (String locale) {
return "$host/input_completion/$locale/";
});

List<LocaleDataItem> jaData = await fetcher.fetch(locale);
print("Fetched ${jaData.length} items: $jaData");

Generator generator = Generator();
generator.loadData(locale, jaData);
try {
List<LocaleDataItem> localeData = await fetcher.fetch(locale);
print("Fetched ${localeData.length} items: $localeData");

generator.loadData(locale, localeData);
} on FetchFailedException catch (e) {
print("Failed to fetch data. Exception: $e");
print("ResponseBody: ${e.responseBody}");
return;
} on UnexpectedResponseBodyException catch (e) {
print("Unexpected response body. Exception: $e");
return;
}

while (true) {
print("Enter a keyword to get completions:");
Expand Down
5 changes: 5 additions & 0 deletions lib/obot_completion_generator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
library;

export 'src/generator.dart' show Generator;
export 'src/exceptions.dart'
show
FetcherException,
FetchFailedException,
UnexpectedResponseBodyException;
export 'src/fetcher.dart' show Fetcher;
export 'src/types.dart'
show
Expand Down
33 changes: 33 additions & 0 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class FetcherException implements Exception {
final String message;

FetcherException(this.message);

@override
String toString() {
return 'FetcherException(message: $message)';
}
}

class FetchFailedException extends FetcherException {
final int statusCode;
final String responseBody;

FetchFailedException(super.message, this.statusCode, this.responseBody);

@override
String toString() {
return 'FetchFailedException(message: $message, statusCode: $statusCode)';
}
}

class UnexpectedResponseBodyException extends FetcherException {
final int statusCode;

UnexpectedResponseBodyException(super.message, this.statusCode);

@override
String toString() {
return 'UnexpectedResponseBodyException(message: $message, statusCode: $statusCode)';
}
}
12 changes: 12 additions & 0 deletions lib/src/fetcher.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';

import 'exceptions.dart';
import 'types.dart';

class Fetcher {
Expand Down Expand Up @@ -56,10 +57,21 @@ class Fetcher {
HttpClientResponse response = await request.close();

try {
// レスポンスを読み取る
responseBody = await response.transform(utf8.decoder).join();
} catch (e) {
// UTF-8でデコードできない場合は例外を投げる
throw UnexpectedResponseBodyException(
"Failed to read response from $endpoint", response.statusCode);
} finally {
client.close();
}

if (response.statusCode != 200) {
// ステータスコードが200以外の場合は例外を投げる
throw FetchFailedException("Failed to fetch data from $endpoint",
response.statusCode, responseBody);
}
return responseBody;
}
}

0 comments on commit 8fa7e4c

Please sign in to comment.