Skip to content

Commit

Permalink
fix: Better check for Upload error type in uploadMedias()
Browse files Browse the repository at this point in the history
Previous implementation would throw if the received error do not
contain any `errors` array. this can happen if the error is not thrown
by cozy-client

Also this commit improve type check to ensure the receive error is the
one we expect. Casting objects using `as` should be avoided as this
will never ensure type compatibility
  • Loading branch information
Ldoppea authored and Crash-- committed Aug 3, 2023
1 parent 904270a commit 0f2e355
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/app/domain/backup/helpers/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ export class BackupError extends Error {
}
}

export const isUploadError = (error: unknown): error is UploadError => {
return (
typeof error === 'object' &&
error !== null &&
'statusCode' in error &&
'errors' in error &&
Array.isArray(error.errors)
)
}

export const isQuotaExceededError = (error: UploadError): boolean => {
return (
error.statusCode === 413 &&
error.errors[0].detail === 'The file is too big and exceeds the disk quota'
error.errors[0]?.detail === 'The file is too big and exceeds the disk quota'
)
}

Expand Down
15 changes: 10 additions & 5 deletions src/app/domain/backup/services/uploadMedias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import {
LocalBackupConfig,
ProgressCallback
} from '/app/domain/backup/models'
import { UploadError } from '/app/domain/upload/models'
import { getBackupInfo } from '/app/domain/backup/services/manageBackup'
import { BackupError, isFatalError } from '/app/domain/backup/helpers/error'
import {
BackupError,
isFatalError,
isUploadError
} from '/app/domain/backup/helpers/error'
import { areAlbumsEnabled } from '/app/domain/backup/services/manageAlbums'

import type CozyClient from 'cozy-client'
Expand Down Expand Up @@ -101,13 +104,15 @@ export const uploadMedias = async (
await setMediaAsBackuped(client, mediaToUpload, documentCreated)

log.debug(`✅ ${mediaToUpload.name} set as backuped`)
} catch (e) {
} catch (error) {
log.debug(
`❌ ${mediaToUpload.name} not uploaded or set as backuped correctly`
)
log.debug(e)
log.debug(error)

const error = e as UploadError
if (!isUploadError(error)) {
return
}

if (isFatalError(error)) {
await setBackupAsReady(client)
Expand Down

0 comments on commit 0f2e355

Please sign in to comment.