-
Notifications
You must be signed in to change notification settings - Fork 0
Improve error handling #86
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { CachingBuffettCodeApiClientV3 } from '~/api/v3/caching-client' | |
| import { YearQuarter } from '~/fiscal-periods/year-quarter' | ||
| import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' | ||
| import { YearQuarterRange } from '~/fiscal-periods/year-quarter-range' | ||
| import { ErrorHandler } from '~/services/error-handler' | ||
| import { Setting } from '~/setting' | ||
|
|
||
| export class CsvExporter { | ||
|
|
@@ -28,7 +29,14 @@ export class CsvExporter { | |
|
|
||
| const setting = Setting.load() | ||
| const client = new CachingBuffettCodeApiClientV3(setting.token) | ||
| const companyService = new CompanyService(ticker, client, today) | ||
|
|
||
| let companyService | ||
| try { | ||
| companyService = new CompanyService(ticker, client, today) | ||
| } catch (e) { | ||
| ErrorHandler.handle(e) | ||
| } | ||
|
|
||
| const ondemandQuarterApiPeriodRange = new OndemandApiPeriodRange(companyService) | ||
|
|
||
| let ondemandQuarterApiPeriods = [] | ||
|
|
@@ -48,12 +56,16 @@ export class CsvExporter { | |
| } | ||
|
|
||
| const quarters = [] | ||
| quarterApiPeriods.forEach(period => { | ||
| quarters.push(client.quarter(ticker, YearQuarterParam.fromYearQuarter(period))) | ||
| }) | ||
| ondemandQuarterApiPeriods.forEach(period => { | ||
| quarters.push(client.ondemandQuarter(ticker, YearQuarterParam.fromYearQuarter(period))) | ||
| }) | ||
| try { | ||
| quarterApiPeriods.forEach(period => { | ||
| quarters.push(client.quarter(ticker, YearQuarterParam.fromYearQuarter(period))) | ||
| }) | ||
| ondemandQuarterApiPeriods.forEach(period => { | ||
| quarters.push(client.ondemandQuarter(ticker, YearQuarterParam.fromYearQuarter(period))) | ||
| }) | ||
| } catch (e) { | ||
| ErrorHandler.handle(e) | ||
| } | ||
|
Comment on lines
+59
to
+68
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上です。広めに |
||
|
|
||
| if (!quarters.length) { | ||
| throw new Error('<<指定されたデータを取得できませんでした>>') | ||
|
|
@@ -96,10 +108,16 @@ export class CsvExporter { | |
| const numRows = data.length | ||
| const numColumns = data[0].length | ||
|
|
||
| const ss = SpreadsheetApp.getActiveSpreadsheet() | ||
| const sheet = ss.insertSheet() | ||
| const range = sheet.getRange(1, 1, numRows, numColumns) // starts from A1 | ||
| try { | ||
| const ss = SpreadsheetApp.getActiveSpreadsheet() | ||
| const sheet = ss.insertSheet() | ||
| const range = sheet.getRange(1, 1, numRows, numColumns) // starts from A1 | ||
|
|
||
| range.setValues(data) | ||
| range.setValues(data) | ||
| } catch (e) { | ||
| throw new Error( | ||
| `<<出力中にエラーが発生しました (${e.name}: ${e.message})。改善しない場合はGoogleアカウントのログアウトおよび再ログインをお試しください>>` | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. シートに書き込むタイミングでエラーが出た場合は再ログインを促すようにします。
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 完璧、本来出ないはずではあるが、出るからなこれ… |
||
| ) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { HttpError } from '~/api/http-error' | ||
| import { ApiResponseError, OndemandApiNotEnabledError, UnsupportedTickerError } from '~/custom-functions/error' | ||
| import { InvalidLYLQError, InvalidYearError, InvalidQuarterError } from '~/fiscal-periods/error' | ||
|
|
||
| export class ErrorHandler { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 内部用の |
||
| static handle(e: Error): void { | ||
| if (e instanceof ApiResponseError) { | ||
| throw new Error('<<指定されたデータを取得できませんでした>>') | ||
| } else if (e instanceof OndemandApiNotEnabledError) { | ||
| throw new Error('<<従量課金APIが有効になっていません>>') | ||
| } else if (e instanceof UnsupportedTickerError) { | ||
| throw new Error('<<サポートされていないtickerです>>') | ||
| } else if (e instanceof HttpError) { | ||
| ErrorHandler.handleHttpError(e) | ||
| } else if (e instanceof InvalidLYLQError) { | ||
| throw new Error('<<無効なLY・LQが指定されています>>') | ||
| } else if (e instanceof InvalidYearError) { | ||
| throw new Error(`<<無効な決算年度が指定されています>>`) | ||
| } else if (e instanceof InvalidQuarterError) { | ||
| throw new Error(`<<無効な四半期が指定されています>>`) | ||
| } else { | ||
| console.error('未定義のエラー', e.name, e.message) | ||
| throw new Error( | ||
| `<<未定義のエラーが発生しました (${e.name}: ${e.message})。改善しない場合はGoogleアカウントのログアウトおよび再ログインをお試しください>>` | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 未定義のエラーが出た場合は再ログインを促すようにします。 |
||
| ) | ||
| } | ||
| } | ||
|
|
||
| static handleHttpError(e: HttpError): void { | ||
| if (e.isInvalidTestingRequest()) { | ||
| throw new Error('<<テスト用のAPIキーでは取得できないデータです>>') | ||
| } else if (e.isInvalidTokenRequest()) { | ||
| throw new Error('<<APIキーが有効ではありません>>') | ||
| } else if (e.isMonthlyLimitExceeded()) { | ||
| throw new Error('<<月間リクエスト制限に達しています>>') | ||
| } else if (e.isResourceNotFound()) { | ||
| throw new Error('<<データが見つかりません。tickerや期間に間違いがないかご確認ください>>') | ||
| } else if (e.isApiQuotaExceeded()) { | ||
| throw new Error('<<APIの実行回数が上限に達しました>>') | ||
| } else if (e.isBadRequest()) { | ||
| throw new Error(`<<無効なリクエストです (${e.name}: ${e.message})>>`) | ||
| } else { | ||
| console.error('システムエラー', e.name, e.message) | ||
| throw new Error(`<<システムエラーが発生しました (${e.name}: ${e.message})>>`) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここでエラーが出るとしたら
HttpErrorだけの想定なんですが、どこでなんのエラーが出るかわからないので、未定義なエラーもハンドリングできるようにErrorHandler.handleHttpErrorではなく広めにErrorHandler.handleしてます。