Skip to content

Commit

Permalink
Cover & handle all MonitorEnv API request errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Nov 24, 2023
1 parent 4f66e0b commit 5824105
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 92 deletions.
10 changes: 1 addition & 9 deletions frontend/src/api/administration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { monitorenvApi } from '.'
import { monitorenvApi } from './api'
import { FrontendApiError } from '../libs/FrontendApiError'

import type { Administration } from '@mtes-mct/monitor-ui'

export const ARCHIVE_ADMINISTRATION_ERROR_MESSAGE = [
'Certaines unités de cette administration ne sont pas archivées.',
'Veuillez les archiver pour pouvoir archiver cette administration.'
].join(' ')
export const DELETE_ADMINISTRATION_ERROR_MESSAGE = [
'Des unités sont encore rattachées à cette administration.',
'Veuillez les supprimer avant de pouvoir supprimer cette administration.'
].join(' ')
const GET_ADMINISTRATION_ERROR_MESSAGE = "Nous n'avons pas pu récupérer cette administration."
const GET_ADMINISTRATIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la liste des administrations."

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/controlUnit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { monitorenvApi } from '.'
import { monitorenvApi } from './api'
import { FrontendApiError } from '../libs/FrontendApiError'

import type { ControlUnit } from '@mtes-mct/monitor-ui'
Expand Down
16 changes: 11 additions & 5 deletions frontend/src/api/controlUnitContact.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { monitorenvApi } from '.'
import { monitorenvApi } from './api'
import { FrontendApiError } from '../libs/FrontendApiError'

import type { ControlUnit } from '@mtes-mct/monitor-ui'

const GET_CONTROL_UNIT_CONTACT_ERROR_MESSAGE = "Nous n'avons pas pu récupérer cette contact."
const CREATE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE = "Nous n'avons pas pu créer ce contact."
const DELETE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE = "Nous n'avons pas pu supprimé ce contact."
const GET_CONTROL_UNIT_CONTACT_ERROR_MESSAGE = "Nous n'avons pas pu récupérer ce contact."
const GET_CONTROL_UNIT_CONTACTS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la liste des contacts."
const UPDATE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE = "Nous n'avons pas pu mettre à jour ce contact."

export const monitorenvControlUnitContactApi = monitorenvApi.injectEndpoints({
endpoints: builder => ({
Expand All @@ -14,15 +17,17 @@ export const monitorenvControlUnitContactApi = monitorenvApi.injectEndpoints({
body: newControlUnitContactData,
method: 'POST',
url: `/v1/control_unit_contacts`
})
}),
transformErrorResponse: response => new FrontendApiError(CREATE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE, response)
}),

deleteControlUnitContact: builder.mutation<void, number>({
invalidatesTags: () => [{ type: 'ControlUnits' }],
query: controlUnitContactId => ({
method: 'DELETE',
url: `/v1/control_unit_contacts/${controlUnitContactId}`
})
}),
transformErrorResponse: response => new FrontendApiError(DELETE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE, response)
}),

getControlUnitContact: builder.query<ControlUnit.ControlUnitContact, number>({
Expand All @@ -43,7 +48,8 @@ export const monitorenvControlUnitContactApi = monitorenvApi.injectEndpoints({
body: nextControlUnitContactData,
method: 'PUT',
url: `/v1/control_unit_contacts/${nextControlUnitContactData.id}`
})
}),
transformErrorResponse: response => new FrontendApiError(UPDATE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE, response)
})
})
})
Expand Down
35 changes: 17 additions & 18 deletions frontend/src/api/controlUnitResource.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { monitorenvApi } from '.'
import { ARCHIVE_GENERIC_ERROR_MESSAGE } from './constants'
import { monitorenvApi } from './api'
import { ApiErrorCode, type BackendApiBooleanResponse } from './types'
import { FrontendApiError } from '../libs/FrontendApiError'
import { newUsageError } from '../libs/UsageError'
import { UsageError } from '../libs/UsageError'

import type { ControlUnit } from '@mtes-mct/monitor-ui'

export const ARCHIVE_CONTROL_UNITE_RESOURCE_ERROR_MESSAGE = "Nous n'avons pas pu archiver ce moyen."
const ARCHIVE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE = "Nous n'avons pas pu archiver ce moyen."
const CREATE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE = "Nous n'avons pas pu créer ce moyen."
const CAN_DELETE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE = "Nous n'avons pas pu vérifier si ce moyen est supprimable."
export const DELETE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE =
"Ce moyen est rattaché à des missions. Veuillez l'en détacher avant de le supprimer."
const DELETE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE = "Nous n'avons pas pu supprimé ce moyen."
const GET_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE = "Nous n'avons pas pu récupérer ce moyen."
const GET_CONTROL_UNIT_RESOURCES_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la liste des moyens."
const UPDATE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE = "Nous n'avons pas pu mettre à jour ce moyen."

export const IMPOSSIBLE_CONTROL_UNIT_RESOURCE_DELETION_ERROR_MESSAGE =
"Ce moyen est rattaché à des missions. Veuillez l'en détacher avant de le supprimer."

export const monitorenvControlUnitResourceApi = monitorenvApi.injectEndpoints({
endpoints: builder => ({
Expand All @@ -21,13 +24,7 @@ export const monitorenvControlUnitResourceApi = monitorenvApi.injectEndpoints({
method: 'PUT',
url: `/v1/control_unit_resources/${controlUnitResourceId}/archive`
}),
transformErrorResponse: response => {
if (response.data.type === ApiErrorCode.UNARCHIVED_CHILD) {
return newUsageError(ARCHIVE_CONTROL_UNITE_RESOURCE_ERROR_MESSAGE)
}

return new FrontendApiError(ARCHIVE_GENERIC_ERROR_MESSAGE, response)
}
transformErrorResponse: response => new FrontendApiError(ARCHIVE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE, response)
}),

canDeleteControlUnitResource: builder.query<boolean, number>({
Expand All @@ -37,13 +34,14 @@ export const monitorenvControlUnitResourceApi = monitorenvApi.injectEndpoints({
transformResponse: (response: BackendApiBooleanResponse) => response.value
}),

createControlUnitResource: builder.mutation<void, ControlUnit.NewControlUnitResourceData>({
createControlUnitResource: builder.mutation<undefined, ControlUnit.NewControlUnitResourceData>({
invalidatesTags: () => [{ type: 'ControlUnits' }, { type: 'Stations' }],
query: newControlUnitResourceData => ({
body: newControlUnitResourceData,
method: 'POST',
url: `/v1/control_unit_resources`
})
}),
transformErrorResponse: response => new FrontendApiError(CREATE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE, response)
}),

deleteControlUnitResource: builder.mutation<void, number>({
Expand All @@ -53,8 +51,8 @@ export const monitorenvControlUnitResourceApi = monitorenvApi.injectEndpoints({
url: `/v1/control_unit_resources/${controlUnitResourceId}`
}),
transformErrorResponse: response => {
if (response.data.type === ApiErrorCode.FOREIGN_KEY_CONSTRAINT) {
return newUsageError(DELETE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE)
if (response.responseData.type === ApiErrorCode.FOREIGN_KEY_CONSTRAINT) {
return new UsageError(IMPOSSIBLE_CONTROL_UNIT_RESOURCE_DELETION_ERROR_MESSAGE)
}

return new FrontendApiError(DELETE_CONTROL_UNIT_RESOURCE_ERROR_MESSAGE, response)
Expand All @@ -79,7 +77,8 @@ export const monitorenvControlUnitResourceApi = monitorenvApi.injectEndpoints({
body: nextControlUnitResourceData,
method: 'PUT',
url: `/v1/control_unit_resources/${nextControlUnitResourceData.id}`
})
}),
transformErrorResponse: response => new FrontendApiError(UPDATE_CONTROL_UNIT_CONTACT_ERROR_MESSAGE, response)
})
})
})
Expand Down
24 changes: 15 additions & 9 deletions frontend/src/api/mission.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { monitorenvApi, monitorfishApi } from '.'
import { ApiError } from '../libs/ApiError'
import { monitorenvApi, monitorfishApi } from './api'
import { FrontendApiError } from '../libs/FrontendApiError'

import type { Mission, MissionWithActions } from '../domain/entities/mission/types'
import type { ControlUnit } from '@mtes-mct/monitor-ui'

const GET_MISSION_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la mission"
const GET_ENGAGED_CONTROL_UNITS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les unités en mission"
const CREATE_MISSION_ERROR_MESSAGE = "Nous n'avons pas pu créer la mission."
const DELETE_MISSION_ERROR_MESSAGE = "Nous n'avons pas pu supprimé la mission."
const GET_MISSION_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la mission."
const GET_ENGAGED_CONTROL_UNITS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les unités en mission."
const UPDATE_MISSION_ERROR_MESSAGE = "Nous n'avons pas pu mettre à jour la mission."

export const monitorenvMissionApi = monitorenvApi.injectEndpoints({
endpoints: builder => ({
Expand All @@ -19,7 +22,8 @@ export const monitorenvMissionApi = monitorenvApi.injectEndpoints({
body: mission,
method: 'POST',
url: `/v1/missions`
})
}),
transformErrorResponse: response => new FrontendApiError(CREATE_MISSION_ERROR_MESSAGE, response)
}),

deleteMission: builder.mutation<void, Mission.Mission['id']>({
Expand All @@ -31,18 +35,19 @@ export const monitorenvMissionApi = monitorenvApi.injectEndpoints({
query: id => ({
method: 'DELETE',
url: `/v1/missions/${id}`
})
}),
transformErrorResponse: response => new FrontendApiError(DELETE_MISSION_ERROR_MESSAGE, response)
}),

getEngagedControlUnits: builder.query<ControlUnit.EngagedControlUnits, void>({
query: () => `/v1/missions/engaged_control_units`,
transformErrorResponse: response => new ApiError(GET_ENGAGED_CONTROL_UNITS_ERROR_MESSAGE, response)
transformErrorResponse: response => new FrontendApiError(GET_ENGAGED_CONTROL_UNITS_ERROR_MESSAGE, response)
}),

getMission: builder.query<Mission.Mission, Mission.Mission['id']>({
providesTags: [{ type: 'Missions' }],
query: id => `/v1/missions/${id}`,
transformErrorResponse: response => new ApiError(GET_MISSION_ERROR_MESSAGE, response)
transformErrorResponse: response => new FrontendApiError(GET_MISSION_ERROR_MESSAGE, response)
}),

updateMission: builder.mutation<void, Mission.Mission>({
Expand All @@ -51,7 +56,8 @@ export const monitorenvMissionApi = monitorenvApi.injectEndpoints({
body: mission,
method: 'POST',
url: `/v1/missions/${mission.id}`
})
}),
transformErrorResponse: response => new FrontendApiError(UPDATE_MISSION_ERROR_MESSAGE, response)
})
})
})
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/api/station.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { monitorenvApi } from '.'
import { monitorenvApi } from './api'
import { FrontendApiError } from '../libs/FrontendApiError'

import type { Station } from '@mtes-mct/monitor-ui'

export const DELETE_STATION_ERROR_MESSAGE =
"Cette base est rattachée à des moyens. Veuillez l'en détacher avant de la supprimer ou bien l'archiver."
const GET_STATION_ERROR_MESSAGE = "Nous n'avons pas pu récupérer cette base."
const GET_STATIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la liste des bases."

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/ControlObjective/apis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ascend, identity } from 'ramda'

import { monitorfishApi } from '../../api'
import { monitorfishApi } from '../../api/api'
import { ApiError } from '../../libs/ApiError'

import type { ControlObjective, CreateControlObjectivePayload, UpdateControlObjective } from './types'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../../../../api/controlUnitContact'
import { ConfirmationModal } from '../../../../../components/ConfirmationModal'
import { useMainAppDispatch } from '../../../../../hooks/useMainAppDispatch'
import { FrontendApiError } from '../../../../../libs/FrontendApiError'
import { FrontendError } from '../../../../../libs/FrontendError'
import { Section } from '../shared/Section'
import { TextareaForm } from '../shared/TextareaForm'
Expand Down Expand Up @@ -64,23 +65,31 @@ export function ControlUnitContactList({ controlUnit, onSubmit }: ControlUnitCon
throw new FrontendError('`editedControlUnitContactId` is undefined.')
}

await dispatch(
monitorenvControlUnitContactApi.endpoints.deleteControlUnitContact.initiate(editedControlUnitContactId)
)
try {
await dispatch(
monitorenvControlUnitContactApi.endpoints.deleteControlUnitContact.initiate(editedControlUnitContactId)
).unwrap()

closeDialogsAndModals()
closeForm()
closeDialogsAndModals()
closeForm()
} catch (err) {
FrontendApiError.handleIfAny(err)
}
}, [closeDialogsAndModals, closeForm, dispatch, editedControlUnitContactId])

const createOrUpdateControlUnitContact = useCallback(
async (controlUnitContactFormValues: ControlUnitContactFormValues) => {
if (isNewControlUnitContactFormOpen) {
await createControlUnitContact(controlUnitContactFormValues as ControlUnit.NewControlUnitContactData)
} else {
await updateControlUnitContact(controlUnitContactFormValues as ControlUnit.ControlUnitContactData)
try {
if (isNewControlUnitContactFormOpen) {
await createControlUnitContact(controlUnitContactFormValues as ControlUnit.NewControlUnitContactData)
} else {
await updateControlUnitContact(controlUnitContactFormValues as ControlUnit.ControlUnitContactData)
}

closeForm()
} catch (err) {
FrontendApiError.handleIfAny(err)
}

closeForm()
},
[closeForm, createControlUnitContact, isNewControlUnitContactFormOpen, updateControlUnitContact]
)
Expand Down
Loading

0 comments on commit 5824105

Please sign in to comment.