diff --git a/src/parser.rs b/src/parser.rs index c9e65492..defa65bf 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -21,7 +21,13 @@ pub async fn parse(api: T, input: &str) -> ParseResult { Some(result) => result, }; // その都道府県の市町村名リストを取得 - let prefecture = api.get_prefecture_master(prefecture_name).await.unwrap(); + let prefecture = match api.get_prefecture_master(prefecture_name).await { + Err(error) => return ParseResult { + address: Address::new(prefecture_name, "", "", rest), + error: Some(error) + }, + Ok(result) => result, + }; // 市町村名を特定 let (rest, city_name) = match read_city(rest, prefecture) { None => { @@ -98,6 +104,20 @@ mod parser_tests { ); } + #[tokio::test] + async fn parse_mocked_fail_都道府県マスタの取得に失敗する() { + let api = ApiMock {should_fail: true}; + let result = parse(api, "東京都新宿区西新宿二丁目8-1").await; + assert_eq!(result.address.prefecture, "東京都".to_string()); + assert_eq!(result.address.city, "".to_string()); + assert_eq!(result.address.town, "".to_string()); + assert_eq!(result.address.rest, "新宿区西新宿二丁目8-1".to_string()); + assert_eq!( + result.error.unwrap().error_type, + "ApiError".to_string() + ); + } + #[tokio::test] async fn parse_mocked_fail_市町村名が間違っている場合() { let api = ApiMock { should_fail: false };