Commit f5bac52f authored by Stefan Probst's avatar Stefan Probst
Browse files

fix: add default error handlers

parent 36dfd57f
......@@ -16,6 +16,7 @@ import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -42,6 +43,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -74,12 +76,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -16,6 +16,7 @@ import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -44,6 +45,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -76,12 +78,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -16,6 +16,7 @@ import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -42,6 +43,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -74,12 +76,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -16,6 +16,7 @@ import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -44,6 +45,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -76,12 +78,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -14,6 +14,7 @@ import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -40,6 +41,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -72,12 +74,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -14,6 +14,7 @@ import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -42,6 +43,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -74,12 +76,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -14,6 +14,7 @@ import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -40,6 +41,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -72,12 +74,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -14,6 +14,7 @@ import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -42,6 +43,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
......@@ -74,12 +76,16 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
router.push({ pathname: '/success' })
}
},
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -24,6 +24,7 @@ import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -54,25 +55,34 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
: false
const queryClient = useQueryClient()
const createStep = useCreateStep({
onError() {
onError(error) {
toast.error(
`Failed to ${isAllowedToPublish ? 'publish' : 'submit'} ${stepLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
const createWorkflow = useItemMutation({
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
......@@ -25,6 +25,7 @@ import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { useErrorHandlers } from '@/modules/error/useErrorHandlers'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -57,32 +58,45 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const handleErrors = useErrorHandlers()
const isAllowedToPublish =
user.data?.role !== undefined
? ['administrator', 'moderator'].includes(user.data.role)
: false
const queryClient = useQueryClient()
const createStep = useCreateStep({
onError() {
onError(error) {
toast.error(
`Failed to ${isAllowedToPublish ? 'publish' : 'submit'} ${stepLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
const updateStep = useUpdateStep({
onError() {
onError(error) {
toast.error(
`Failed to ${isAllowedToPublish ? 'publish' : 'submit'} ${stepLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
const updateWorkflow = useItemMutation({
onError() {
onError(error) {
toast.error(
`Failed to ${
isAllowedToPublish ? 'publish' : 'submit'
} ${categoryLabel}.`,
)
if (error instanceof Error) {
handleErrors(error)
}
},
})
......
import { HttpError } from '@/api/sshoc'
import { useAuth } from '@/modules/auth/AuthContext'
export function useErrorHandlers(): (error: Error) => void {
const auth = useAuth()
function handle(error: Error) {
if (error instanceof HttpError) {
switch (error.statusCode) {
case 401:
case 403:
auth.signOut()
break
}
}
}
return handle
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment