chore: convert all errors to named exports (#6061)

This commit is contained in:
Elliot DeNolf
2024-04-26 13:24:18 -04:00
committed by GitHub
parent a4e8795666
commit 739dfc1434
32 changed files with 73 additions and 119 deletions

View File

@@ -7,7 +7,7 @@ import baseAccountLockFields from '../../auth/baseFields/accountLock.js'
import baseAPIKeyFields from '../../auth/baseFields/apiKey.js'
import baseAuthFields from '../../auth/baseFields/auth.js'
import baseVerificationFields from '../../auth/baseFields/verification.js'
import TimestampsRequired from '../../errors/TimestampsRequired.js'
import { TimestampsRequired } from '../../errors/TimestampsRequired.js'
import { sanitizeFields } from '../../fields/config/sanitize.js'
import { fieldAffectsData } from '../../fields/config/types.js'
import mergeBaseFields from '../../fields/mergeBaseFields.js'

View File

@@ -6,7 +6,7 @@ import type { SanitizedGlobalConfig } from '../../globals/config/types.js'
import type { Operator, PayloadRequestWithData, Where, WhereField } from '../../types/index.js'
import type { EntityPolicies } from './types.js'
import QueryError from '../../errors/QueryError.js'
import { QueryError } from '../../errors/QueryError.js'
import { validOperators } from '../../types/constants.js'
import { deepCopyObject } from '../../utilities/deepCopyObject.js'
import flattenFields from '../../utilities/flattenTopLevelFields.js'

View File

@@ -26,7 +26,7 @@ class ExtendableError<TData extends object = { [key: string]: unknown }> extends
* Class representing an API error.
* @extends ExtendableError
*/
class APIError<
export class APIError<
TData extends null | object = { [key: string]: unknown } | null,
> extends ExtendableError<TData> {
/**
@@ -45,5 +45,3 @@ class APIError<
super(message, status, data, isPublic)
}
}
export default APIError

View File

@@ -3,9 +3,9 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class AuthenticationError extends APIError {
export class AuthenticationError extends APIError {
constructor(t?: TFunction) {
super(
t ? t('error:emailOrPasswordIncorrect') : en.translations.error.emailOrPasswordIncorrect,
@@ -13,5 +13,3 @@ class AuthenticationError extends APIError {
)
}
}
export default AuthenticationError

View File

@@ -1,9 +1,7 @@
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class DuplicateCollection extends APIError {
export class DuplicateCollection extends APIError {
constructor(propertyName: string, duplicates: string[]) {
super(`Collection ${propertyName} already in use: "${duplicates.join(', ')}"`)
}
}
export default DuplicateCollection

View File

@@ -1,11 +1,9 @@
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class DuplicateFieldName extends APIError {
export class DuplicateFieldName extends APIError {
constructor(fieldName: string) {
super(
`A field with the name '${fieldName}' was found multiple times on the same level. Field names must be unique.`,
)
}
}
export default DuplicateFieldName

View File

@@ -1,11 +1,9 @@
import type { GlobalConfig } from '../globals/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class DuplicateGlobal extends APIError {
export class DuplicateGlobal extends APIError {
constructor(config: GlobalConfig) {
super(`Global label "${config.label}" is already in use`)
}
}
export default DuplicateGlobal

View File

@@ -3,9 +3,9 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class ErrorDeletingFile extends APIError {
export class ErrorDeletingFile extends APIError {
constructor(t?: TFunction) {
super(
t ? t('error:deletingFile') : en.translations.error.deletingFile,
@@ -13,5 +13,3 @@ class ErrorDeletingFile extends APIError {
)
}
}
export default ErrorDeletingFile

View File

@@ -2,7 +2,7 @@ import type { TFunction } from '@payloadcms/translations'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
export class FileRetrievalError extends APIError {
constructor(t?: TFunction, message?: string) {

View File

@@ -3,9 +3,9 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class FileUploadError extends APIError {
export class FileUploadError extends APIError {
constructor(t?: TFunction) {
super(
t ? t('error:problemUploadingFile') : en.translations.error.problemUploadingFile,
@@ -13,5 +13,3 @@ class FileUploadError extends APIError {
)
}
}
export default FileUploadError

View File

@@ -3,9 +3,9 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class Forbidden extends APIError {
export class Forbidden extends APIError {
constructor(t?: TFunction) {
super(
t ? t('error:notAllowedToPerformAction') : en.translations.error.notAllowedToPerformAction,
@@ -13,5 +13,3 @@ class Forbidden extends APIError {
)
}
}
export default Forbidden

View File

@@ -1,11 +1,9 @@
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class InvalidConfiguration extends APIError {
export class InvalidConfiguration extends APIError {
constructor(message: string) {
super(message, httpStatus.INTERNAL_SERVER_ERROR)
}
}
export default InvalidConfiguration

View File

@@ -1,13 +1,11 @@
import type { FieldAffectingData } from '../fields/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class InvalidFieldName extends APIError {
export class InvalidFieldName extends APIError {
constructor(field: FieldAffectingData, fieldName: string) {
super(
`Field ${field.label} has invalid name '${fieldName}'. Field names can not include periods (.) and must be alphanumeric.`,
)
}
}
export default InvalidFieldName

View File

@@ -1,11 +1,9 @@
import type { RelationshipField, UploadField } from '../fields/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class InvalidFieldRelationship extends APIError {
export class InvalidFieldRelationship extends APIError {
constructor(field: RelationshipField | UploadField, relationship: string) {
super(`Field ${field.label} has invalid relationship '${relationship}'.`)
}
}
export default InvalidFieldRelationship

View File

@@ -1,11 +1,9 @@
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class InvalidSchema extends APIError {
export class InvalidSchema extends APIError {
constructor(message: string, results: any) {
super(message, httpStatus.INTERNAL_SERVER_ERROR, results)
}
}
export default InvalidSchema

View File

@@ -3,12 +3,10 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class LockedAuth extends APIError {
export class LockedAuth extends APIError {
constructor(t?: TFunction) {
super(t ? t('error:userLocked') : en.translations.error.userLocked, httpStatus.UNAUTHORIZED)
}
}
export default LockedAuth

View File

@@ -1,9 +1,7 @@
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class MissingCollectionLabel extends APIError {
export class MissingCollectionLabel extends APIError {
constructor() {
super('payload.config.collection object is missing label')
}
}
export default MissingCollectionLabel

View File

@@ -1,14 +1,12 @@
import type { Field } from '../fields/config/types.js'
import { fieldAffectsData } from '../fields/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class MissingEditorProp extends APIError {
export class MissingEditorProp extends APIError {
constructor(field: Field) {
super(
`RichText field${fieldAffectsData(field) ? ` "${field.name}"` : ''} is missing the editor prop`,
)
}
}
export default MissingEditorProp

View File

@@ -1,11 +1,9 @@
import type { RadioField, SelectField } from '../fields/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class MissingFieldInputOptions extends APIError {
export class MissingFieldInputOptions extends APIError {
constructor(field: RadioField | SelectField) {
super(`Field ${field.label} is missing options.`)
}
}
export default MissingFieldInputOptions

View File

@@ -1,9 +1,9 @@
import type { Field } from '../fields/config/types.js'
import { fieldAffectsData } from '../fields/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class MissingFieldType extends APIError {
export class MissingFieldType extends APIError {
constructor(field: Field) {
super(
`Field${
@@ -12,5 +12,3 @@ class MissingFieldType extends APIError {
)
}
}
export default MissingFieldType

View File

@@ -3,9 +3,9 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class MissingFile extends APIError {
export class MissingFile extends APIError {
constructor(t?: TFunction) {
super(
t ? t('error:noFilesUploaded') : en.translations.error.noFilesUploaded,
@@ -13,5 +13,3 @@ class MissingFile extends APIError {
)
}
}
export default MissingFile

View File

@@ -3,12 +3,10 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class NotFound extends APIError {
export class NotFound extends APIError {
constructor(t?: TFunction) {
super(t ? t('general:notFound') : en.translations.general.notFound, httpStatus.NOT_FOUND)
}
}
export default NotFound

View File

@@ -1,8 +1,8 @@
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class QueryError extends APIError<{ path: string }[]> {
export class QueryError extends APIError<{ path: string }[]> {
constructor(results: { path: string }[]) {
const message = `The following path${results.length === 1 ? '' : 's'} cannot be queried:`
@@ -13,5 +13,3 @@ class QueryError extends APIError<{ path: string }[]> {
)
}
}
export default QueryError

View File

@@ -1,13 +1,11 @@
import type { CollectionConfig } from '../collections/config/types.js'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class TimestampsRequired extends APIError {
export class TimestampsRequired extends APIError {
constructor(collection: CollectionConfig) {
super(
`Timestamps are required in the collection ${collection.slug} because you have opted in to Versions.`,
)
}
}
export default TimestampsRequired

View File

@@ -3,12 +3,10 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class UnauthorizedError extends APIError {
export class UnauthorizedError extends APIError {
constructor(t?: TFunction) {
super(t ? t('error:unauthorized') : en.translations.error.unauthorized, httpStatus.UNAUTHORIZED)
}
}
export default UnauthorizedError

View File

@@ -3,9 +3,9 @@ import type { TFunction } from '@payloadcms/translations'
import { en } from '@payloadcms/translations/languages/en'
import httpStatus from 'http-status'
import APIError from './APIError.js'
import { APIError } from './APIError.js'
class ValidationError extends APIError<{ field: string; message: string }[]> {
export class ValidationError extends APIError<{ field: string; message: string }[]> {
constructor(results: { field: string; message: string }[], t?: TFunction) {
const message = t
? t('error:followingFieldsInvalid', { count: results.length })
@@ -16,5 +16,3 @@ class ValidationError extends APIError<{ field: string; message: string }[]> {
super(`${message} ${results.map((f) => f.field).join(', ')}`, httpStatus.BAD_REQUEST, results)
}
}
export default ValidationError

View File

@@ -1,20 +1,20 @@
export { default as APIError } from './APIError.js'
export { default as AuthenticationError } from './AuthenticationError.js'
export { default as DuplicateCollection } from './DuplicateCollection.js'
export { default as DuplicateFieldName } from './DuplicateFieldName.js'
export { default as DuplicateGlobal } from './DuplicateGlobal.js'
export { default as ErrorDeletingFile } from './ErrorDeletingFile.js'
export { APIError } from './APIError.js'
export { AuthenticationError } from './AuthenticationError.js'
export { DuplicateCollection } from './DuplicateCollection.js'
export { DuplicateFieldName } from './DuplicateFieldName.js'
export { DuplicateGlobal } from './DuplicateGlobal.js'
export { ErrorDeletingFile } from './ErrorDeletingFile.js'
export { FileRetrievalError } from './FileRetrievalError.js'
export { default as FileUploadError } from './FileUploadError.js'
export { default as Forbidden } from './Forbidden.js'
export { default as InvalidConfiguration } from './InvalidConfiguration.js'
export { default as InvalidFieldName } from './InvalidFieldName.js'
export { default as InvalidFieldRelationship } from './InvalidFieldRelationship.js'
export { default as LockedAuth } from './LockedAuth.js'
export { default as MissingCollectionLabel } from './MissingCollectionLabel.js'
export { default as MissingFieldInputOptions } from './MissingFieldInputOptions.js'
export { default as MissingFieldType } from './MissingFieldType.js'
export { default as MissingFile } from './MissingFile.js'
export { default as NotFound } from './NotFound.js'
export { default as QueryError } from './QueryError.js'
export { default as ValidationError } from './ValidationError.js'
export { FileUploadError } from './FileUploadError.js'
export { Forbidden } from './Forbidden.js'
export { InvalidConfiguration } from './InvalidConfiguration.js'
export { InvalidFieldName } from './InvalidFieldName.js'
export { InvalidFieldRelationship } from './InvalidFieldRelationship.js'
export { LockedAuth } from './LockedAuth.js'
export { MissingCollectionLabel } from './MissingCollectionLabel.js'
export { MissingFieldInputOptions } from './MissingFieldInputOptions.js'
export { MissingFieldType } from './MissingFieldType.js'
export { MissingFile } from './MissingFile.js'
export { NotFound } from './NotFound.js'
export { QueryError } from './QueryError.js'
export { ValidationError } from './ValidationError.js'

View File

@@ -1,7 +1,7 @@
import type { Config } from '../../config/types.js'
import type { Field } from './types.js'
import MissingEditorProp from '../../errors/MissingEditorProp.js'
import { MissingEditorProp } from '../../errors/MissingEditorProp.js'
import {
DuplicateFieldName,
InvalidFieldName,

View File

@@ -3,8 +3,8 @@ import type { PreferenceRequest } from '../types.js'
import defaultAccess from '../../auth/defaultAccess.js'
import executeAccess from '../../auth/executeAccess.js'
import NotFound from '../../errors/NotFound.js'
import UnauthorizedError from '../../errors/UnathorizedError.js'
import { NotFound } from '../../errors/NotFound.js'
import { UnauthorizedError } from '../../errors/UnathorizedError.js'
async function deleteOperation(args: PreferenceRequest): Promise<Document> {
const {

View File

@@ -2,7 +2,7 @@ import type { PreferenceUpdateRequest } from '../types.js'
import defaultAccess from '../../auth/defaultAccess.js'
import executeAccess from '../../auth/executeAccess.js'
import UnauthorizedError from '../../errors/UnathorizedError.js'
import { UnauthorizedError } from '../../errors/UnathorizedError.js'
async function update(args: PreferenceUpdateRequest) {
const {

View File

@@ -1,6 +1,6 @@
import type { SanitizedCollectionConfig } from '../collections/config/types.js'
import DuplicateCollection from '../errors/DuplicateCollection.js'
import { DuplicateCollection } from '../errors/DuplicateCollection.js'
const getDuplicates = (arr: string[]) => arr.filter((item, index) => arr.indexOf(item) !== index)

View File

@@ -1,4 +1,4 @@
import APIError from '../errors/APIError.js'
import { APIError } from '../errors/APIError.js'
export const parseCookies = (headers: Request['headers']): Map<string, string> => {
const list = new Map<string, string>()