Skip to content
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

When upload has errors show them to user #143

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

* Upload errors are rendered on screen instead of console ([#143](https://github.com/i-VRESSE/workflow-builder/pull/143))
* In table moved buttons from last to first column ([#141](https://github.com/i-VRESSE/workflow-builder/issues/141))

## @i-vresse/wb-core 2.0.0 - 2024-03-18

### Removed
Expand Down
6 changes: 3 additions & 3 deletions apps/haddock3-submit/src/useSubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ interface Data extends IWorkflow {
files: IFiles
}

interface Subission {
interface Submission {
progressUrl: string
}

async function submit2api (url: string, data: Data): Promise<Subission> {
async function submit2api (url: string, data: Data): Promise<Submission> {
const resp = await fetch(
url,
{
Expand Down Expand Up @@ -38,7 +38,7 @@ export const useSubmit: (url?: string) => () => Promise<void> = (url = '/api/run
{
pending: 'Submitting workfow ...',
success: {
render ({ data }: { data: Subission}) {
render ({ data }: { data: Submission}) {
// TODO instead of presenting link could redirect to progress page
return (
<div>
Expand Down
79 changes: 58 additions & 21 deletions packages/core/src/WorkflowUploadButton.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,77 @@
import React, { useRef } from 'react'
import { toast } from 'react-toastify'
import { UpdateOptions, toast } from 'react-toastify'
import { useWorkflow } from './store'
import { ValidationError, flattenValidationErrors } from './validate'

function ErrorsList ({ error }: { error: ValidationError }): JSX.Element {
return (
<>
<div>Workflow archive failed to load.</div>
<ul style={{ maxHeight: '20rem', overflow: 'auto' }}>
{flattenValidationErrors(error).map((message, index) => (
<li key={index}>{message}</li>
))}
</ul>
</>
)
}

export const WorkflowUploadButton = (): JSX.Element => {
const uploadRef = useRef<HTMLInputElement>(null)
const { loadWorkflowArchive } = useWorkflow()

async function uploadWorkflow (event: React.ChangeEvent<HTMLInputElement>): Promise<void> {
async function uploadWorkflow (
event: React.ChangeEvent<HTMLInputElement>
): Promise<void> {
if (event.target.files == null) {
return
}
const file = event.target.files[0]
const url = URL.createObjectURL(file)
await toast.promise(
async () => {
try {
await loadWorkflowArchive(url)
} finally {
URL.revokeObjectURL(url)
}
},
{
pending: 'Loading workfow ...',
success: 'Workflow loaded',
error: {
render ({ data }) {
console.error(data)
return 'Workflow archive failed to load. See DevTools (F12) console for errors.'
}
}
const toastId = toast.loading('Loading workfow ...')
try {
await loadWorkflowArchive(url)
toast.update(toastId, {
type: 'success',
render: 'Workflow loaded',
autoClose: 1000,
isLoading: false,
progress: 1
})
toast.dismiss(toastId)
} catch (error) {
const opts: Partial<UpdateOptions> = {
type: 'error',
autoClose: false,
closeOnClick: true,
closeButton: true,
isLoading: false,
progress: 1
}
if (error instanceof ValidationError) {
toast.update(toastId, {
...opts,
render: <ErrorsList error={error} />
})
} else {
console.error(error)
toast.update(toastId, {
...opts,
render:
'Workflow archive failed to load. See DevTools (F12) console for errors.'
})
}
)
} finally {
URL.revokeObjectURL(url)
}
}

return (
<button className='btn btn-light' onClick={() => uploadRef.current?.click()} title='Upload an archive'>
<button
className='btn btn-light'
onClick={() => uploadRef.current?.click()}
title='Upload an archive'
>
Upload
<input
type='file'
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,21 @@ export function validateCatalog (catalog: ICatalog): Errors {
// TODO validate non schema fields
return [...globalErrors, ...nodesErrors.flat(1)]
}

export function flattenValidationErrors (error: ValidationError): string[] {
return error.errors.map((e) => {
if (e.workflowPath === undefined || e.message === undefined) {
return ''
}
let message = e.message
message = `Error in ${e.workflowPath}${e.instancePath}: ${message}`
if (typeof e.params.additionalProperty === 'string') {
message += `: ${e.params.additionalProperty}`
}
if (Array.isArray(e.params.allowedValues)) {
const values = e.params.allowedValues.map(d => String(d))
message += `: ${values.join(', ')}`
}
return message
}).filter(m => m !== '')
}
1 change: 0 additions & 1 deletion packages/core/src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export async function loadWorkflowArchive (archiveURL: string, catalog: ICatalog
const workflow = parseWorkflow(tomlstring, catalog)
const errors = await validateWorkflow(workflow, catalog, files)
if (errors.length > 0) {
console.error(errors)
throw new ValidationError('Invalid workflow loaded', errors)
}
return {
Expand Down
Loading