-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [Refactor] Replace console.error with proper error handling in CalendarScreen #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,7 +23,8 @@ import { | |||||||||||||||
| getPdfExtractorHtml, parseShiftCells, | ||||||||||||||||
| type ParsedSchedule, type ParsedEmployee, type ParsedShift, | ||||||||||||||||
| } from '../utils/pdfShiftParser'; | ||||||||||||||||
| import { useLanguage } from '../context/LanguageContext'; | ||||||||||||||||
| import { useLanguage } from '../context/LanguageContext'; | ||||||||||||||||
| import { handleError } from '../utils/errorHandler'; | ||||||||||||||||
|
|
||||||||||||||||
| const STORAGE_KEY = '@shift_import_name'; | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -111,7 +112,7 @@ export default function CalendarScreen() { | |||||||||||||||
| setManualModalOpen(false); | ||||||||||||||||
| fetchCalendar(true); | ||||||||||||||||
| Alert.alert(t('calShiftSaved')); | ||||||||||||||||
| } catch (e: any) { Alert.alert('Errore', e.message); } | ||||||||||||||||
| } catch (e: unknown) { handleError(e, 'calendar'); } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| const SCREEN_W = Dimensions.get('window').width; | ||||||||||||||||
|
|
@@ -198,7 +199,7 @@ export default function CalendarScreen() { | |||||||||||||||
| setEventsData(localData); | ||||||||||||||||
| setLoading(false); | ||||||||||||||||
| fetchWeatherAndFlights(start, end, localData); | ||||||||||||||||
| } catch (e) { if (__DEV__) console.error(e); setLoading(false); } | ||||||||||||||||
| } catch (e: unknown) { handleError(e, 'calendar'); setLoading(false); } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| const fetchWeatherAndFlights = async (start: Date, end: Date, localData: Record<string, ShiftEvent[]>) => { | ||||||||||||||||
|
|
@@ -216,7 +217,7 @@ export default function CalendarScreen() { | |||||||||||||||
| dict[date] = { weatherText: m.text, weatherIcon: m.icon, flightCount: 0 }; | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| } catch (e) { if (__DEV__) console.warn('[calWeather]', e); } | ||||||||||||||||
| } catch (e: unknown) { handleError(e, 'network', true); } | ||||||||||||||||
| try { | ||||||||||||||||
| const { arrivals, departures } = await fetchAirportScheduleRaw(airportCode); | ||||||||||||||||
| const allF = [...arrivals, ...departures]; | ||||||||||||||||
|
|
@@ -232,7 +233,7 @@ export default function CalendarScreen() { | |||||||||||||||
| if (dict[iso]) dict[iso].flightCount = cnt; else dict[iso] = { weatherText: 'N/A', weatherIcon: '❓', flightCount: cnt }; | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
| } catch (e) { if (__DEV__) console.warn('[calFlights]', e); } | ||||||||||||||||
| } catch (e: unknown) { handleError(e, 'flight', true); } | ||||||||||||||||
| setDailyStats(dict); | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -261,9 +262,8 @@ export default function CalendarScreen() { | |||||||||||||||
| setImportStep('extracting'); | ||||||||||||||||
| setImportModalVisible(true); | ||||||||||||||||
| setPdfHtml(getPdfExtractorHtml(base64)); | ||||||||||||||||
| } catch (e: any) { | ||||||||||||||||
| if (__DEV__) console.error(`Import error at step=${step}:`, e); | ||||||||||||||||
| Alert.alert('Errore', `Errore (${step}): ${e?.message || e}`); | ||||||||||||||||
| } catch (e: unknown) { | ||||||||||||||||
| handleError(e instanceof Error ? new Error(`Errore (${step}): ${e.message}`, { cause: e }) : e, 'import'); | ||||||||||||||||
| } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -302,9 +302,8 @@ export default function CalendarScreen() { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| setImportStep('pickName'); | ||||||||||||||||
| } catch (e) { | ||||||||||||||||
| if (__DEV__) console.error(e); | ||||||||||||||||
| Alert.alert('Errore', 'Errore nel parsing del PDF'); | ||||||||||||||||
| } catch (e: unknown) { | ||||||||||||||||
| handleError(e instanceof Error ? new Error('Errore nel parsing del PDF', { cause: e }) : e, 'import'); | ||||||||||||||||
| setImportModalVisible(false); | ||||||||||||||||
| setImportStep('idle'); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+305
to
309
|
||||||||||||||||
|
|
@@ -348,9 +347,8 @@ export default function CalendarScreen() { | |||||||||||||||
| fetchCalendar(true); | ||||||||||||||||
| Alert.alert(t('calImportComplete'), `${saved} turni salvati nel calendario`); | ||||||||||||||||
| }, 800); | ||||||||||||||||
| } catch (e) { | ||||||||||||||||
| if (__DEV__) console.error(e); | ||||||||||||||||
| Alert.alert('Errore', t('calImportError')); | ||||||||||||||||
| } catch (e: unknown) { | ||||||||||||||||
| handleError(e instanceof Error ? new Error(t('calImportError'), { cause: e }) : e, 'import'); | ||||||||||||||||
|
||||||||||||||||
| handleError(e instanceof Error ? new Error(t('calImportError'), { cause: e }) : e, 'import'); | |
| handleError( | |
| new Error(t('calImportError'), { | |
| cause: e instanceof Error ? e : new Error(String(e)), | |
| }), | |
| 'import', | |
| ); |
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.
In this catch, the user-facing message includes the
steponly when the thrown value is anError. If something throws a non-Error(string/object),handleErrorwill end up alertingString(e)and thestepcontext is lost compared to the previous behavior. Consider always wrapping into a newErrorthat includesstep(and attach the original value ascause) so the alert stays consistent and actionable.