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 committed Aug 3, 2023
1 parent d0cfea6 commit 0dd6468
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 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
10 changes: 6 additions & 4 deletions src/app/domain/backup/services/uploadMedias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '/app/domain/backup/models'
import { UploadError } from '/app/domain/upload/models'

Check failure on line 14 in src/app/domain/backup/services/uploadMedias.ts

View workflow job for this annotation

GitHub Actions / quality_checks

'UploadError' is defined but never used
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'

Check failure on line 16 in src/app/domain/backup/services/uploadMedias.ts

View workflow job for this annotation

GitHub Actions / quality_checks

Replace `·BackupError,·isFatalError,·isUploadError·` with `⏎··BackupError,⏎··isFatalError,⏎··isUploadError⏎`
import { areAlbumsEnabled } from '/app/domain/backup/services/manageAlbums'

import type CozyClient from 'cozy-client'
Expand Down Expand Up @@ -101,13 +101,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 0dd6468

Please sign in to comment.