Skip to content

Commit

Permalink
Fix/api changes: refactored error handling and minor fixes (#19)
Browse files Browse the repository at this point in the history
* Fix(SignalEntryForm): added missing attributes for updating signal

* Fix(api_config): automatic localStorage clearing if interceptor fails to refresh token

* Refactor(errors): error handling message restructured to fit new response
  • Loading branch information
GedionT authored and mykolaskrynnyk committed Nov 6, 2024
1 parent 39e6afe commit e63c821
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/AdminPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function AdminPanel() {
setTotalCount(0);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
8 changes: 4 additions & 4 deletions src/Components/AddSignalsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function AddSignalsModal(props: Props) {
setLoading(false);
} else if (err.response?.status === 500) {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand All @@ -98,7 +98,7 @@ export function AddSignalsModal(props: Props) {
setLoading(false);
} else if (err.response?.status === 422) {
setError(
`Error code ${err.response?.status}: ${
`${err}. ${
err.response?.status === 422
? 'Please check if the input to this field is correct, IDs are numerical and must be separated by commas'
: ''
Expand Down Expand Up @@ -200,7 +200,7 @@ export function AddSignalsModal(props: Props) {
setLoading(false);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -289,7 +289,7 @@ export function AddSignalsModal(props: Props) {
setLoading(false);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
8 changes: 4 additions & 4 deletions src/Components/AddTrendsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function AddTrendsModal(props: Props) {
setLoading(false);
} else if (err.response?.status === 500) {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand All @@ -95,7 +95,7 @@ export function AddTrendsModal(props: Props) {
setLoading(false);
} else if (err.response?.status === 422) {
setError(
`Error code ${err.response?.status}: ${
`${err}. ${
err.response?.status === 422
? 'Please check if the input to this field is correct, IDs are numerical and must be separated by commas'
: ''
Expand Down Expand Up @@ -180,7 +180,7 @@ export function AddTrendsModal(props: Props) {
setLoading(false);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -210,7 +210,7 @@ export function AddTrendsModal(props: Props) {
setLoading(false);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
4 changes: 1 addition & 3 deletions src/Components/SignOutButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,7 @@ export function SignOutButton(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
42 changes: 17 additions & 25 deletions src/Components/SignalEntryFormEl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function SignalEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500 ? 'Please try again in some time' : ''
}`,
);
Expand All @@ -224,7 +224,7 @@ export function SignalEntryFormEl(props: Props) {
})
.catch(err => {
setSubmittingError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -989,7 +989,8 @@ export function SignalEntryFormEl(props: Props) {
relevance: signalData?.relevance || '',
location: signalData?.location || '',
created_by: signalData?.created_by || '',
score: signalData?.score || '',
created_for: signalData.created_for,
score: signalData.score,
connected_trends: selectedTrendsList,
status: 'New',
keywords: [keyword1, keyword2, keyword3].filter(
Expand All @@ -1006,9 +1007,7 @@ export function SignalEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand All @@ -1032,17 +1031,18 @@ export function SignalEntryFormEl(props: Props) {
id: signalData.id,
headline: signalData?.headline || '',
description: signalData?.description || '',
attachment: signalData?.attachment || '',
attachment: signalData?.attachment,
steep_primary: signalData?.steep_primary || '',
steep_secondary: signalData?.steep_secondary,
signature_primary: signalData?.signature_primary || '',
signature_secondary:
signalData?.signature_secondary || [],
signature_secondary: signalData?.signature_secondary,
sdgs: signalData?.sdgs || [],
url: signalData?.url || '',
relevance: signalData?.relevance || '',
location: signalData?.location || '',
score: signalData?.score || '',
score: signalData.score,
created_by: signalData?.created_by || '',
created_for: signalData.created_for,
connected_trends: selectedTrendsList,
keywords: [keyword1, keyword2, keyword3].filter(
(d): d is string => d !== null && d !== undefined,
Expand All @@ -1059,9 +1059,7 @@ export function SignalEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -1128,7 +1126,7 @@ export function SignalEntryFormEl(props: Props) {
status: signalData?.status || '',
created_by: signalData.created_by || '',
created_for: signalData?.created_for || '',
score: signalData?.score || '',
score: signalData.score,
created_unit: signalData?.created_unit || '',
connected_trends: selectedTrendsList,
keywords: [keyword1, keyword2, keyword3].filter(
Expand All @@ -1143,9 +1141,7 @@ export function SignalEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -1192,7 +1188,7 @@ export function SignalEntryFormEl(props: Props) {
relevance: signalData?.relevance || '',
created_for: signalData?.created_for || '',
location: signalData?.location || '',
score: signalData?.score || '',
score: signalData.score,
connected_trends: selectedTrendsList,
status: 'New',
keywords: [keyword1, keyword2, keyword3].filter(
Expand All @@ -1209,9 +1205,7 @@ export function SignalEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -1241,7 +1235,7 @@ export function SignalEntryFormEl(props: Props) {
url: signalData?.url || '',
relevance: signalData?.relevance || '',
created_for: signalData?.created_for || '',
score: signalData?.score || '',
score: signalData.score,
location: signalData?.location || '',
connected_trends: selectedTrendsList,
status: 'Draft',
Expand All @@ -1259,9 +1253,7 @@ export function SignalEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
10 changes: 3 additions & 7 deletions src/Components/TrendEntryFormEl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function TrendEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500 ? 'Please try again in some time' : ''
}`,
);
Expand Down Expand Up @@ -736,9 +736,7 @@ export function TrendEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -780,9 +778,7 @@ export function TrendEntryFormEl(props: Props) {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
2 changes: 1 addition & 1 deletion src/HomePage/TrendsVis/Viz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function Viz(props: Props) {
setTrendsList([]);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
4 changes: 2 additions & 2 deletions src/MyDrafts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function MyDrafts() {
updateSignalList([]);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down Expand Up @@ -66,7 +66,7 @@ export function MyDrafts() {
updateSignalList([]);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
4 changes: 2 additions & 2 deletions src/Signals/AllSignals/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function AllSignals(props: Props) {
setTotalCount(0);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}: ${err.response?.data}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand All @@ -142,7 +142,7 @@ export function AllSignals(props: Props) {
setTotalCount(0);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
4 changes: 1 addition & 3 deletions src/Signals/SignalDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,7 @@ export function SignalDetail() {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
4 changes: 2 additions & 2 deletions src/Trends/AllTrends/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function AllTrends(props: Props) {
setTotalCount(0);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand All @@ -133,7 +133,7 @@ export function AllTrends(props: Props) {
setTotalCount(0);
} else {
setError(
`Error code ${err.response?.status}: ${err.response?.data}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
2 changes: 1 addition & 1 deletion src/Trends/EditTrend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function EditTrend() {
})
.catch(err => {
setError(
`Error code ${err.response?.status}: ${
`${err}: ${
err.response?.status === 404
? 'No trend available with the selected ID'
: err.response?.data
Expand Down
6 changes: 2 additions & 4 deletions src/Trends/TrendDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function TrendDetail() {
})
.catch(err => {
setError(
`Error code ${err.response?.status}: ${
`${err}: ${
err.response?.status === 404
? 'No trend available with the selected IDs'
: err.response?.data
Expand Down Expand Up @@ -357,9 +357,7 @@ export function TrendDetail() {
.catch(err => {
setButtonDisabled(false);
setSubmittingError(
`Error code ${err.response?.status}: ${
err.response?.data
}. ${
`${err}. ${
err.response?.status === 500
? 'Please try again in some time'
: ''
Expand Down
1 change: 1 addition & 0 deletions src/api/api_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ axiosInstance.interceptors.request.use(
throw new Error('Failed to refresh access token');
}
} catch (error) {
localStorage.clear();
await signOutClickHandler();
throw error;
}
Expand Down

0 comments on commit e63c821

Please sign in to comment.