bde148f286
* wip * some extra fixes * stuff kinda works? * need to figure out mocks * need to figure out mocks * fix db listener * gqlgen fix * minor gqlgen watch adjustment * lint fixes * delete old codegen file * converting migrations to ESM * getModuleDIrectory * vitest sort of works * added back ts-vitest * resolve gql double load * fixing test timeout configs * TSC lint fix * fix automate tests * moar debugging * debugging * more debugging * codegen update * server works * yargs migrated * chore(server): getting rid of global mocks for Server ESM (#5046) * got rid of email mock * got rid of comment mocks * got rid of multi region mocks * got rid of stripe mock * admin override mock updated * removed final mock * fixing import.meta.resolve calls * another import.meta.resolve fix * added requested test * nyc ESM fix * removed unneeded deps + linting * yarn lock forgot to commit * tryna fix flakyness * email capture util fix * sendEmail fix * fix TSX check * sender transporter fix + CR comments * merge main fix * test fixx * circleci fix * gqlgen bigint fix * error formatter fix * more error formatting improvements * esmloader added to Dockerfile * more dockerfile fixes * bg jobs fix
77 lines
2.6 KiB
TypeScript
77 lines
2.6 KiB
TypeScript
import { StreamNotFoundError } from '@/modules/core/errors/stream'
|
|
import { WorkspacesModuleDisabledError } from '@/modules/core/errors/workspaces'
|
|
import {
|
|
BadRequestError,
|
|
BaseError,
|
|
ForbiddenError,
|
|
NotFoundError
|
|
} from '@/modules/shared/errors'
|
|
import { SsoSessionMissingOrExpiredError } from '@/modules/workspacesCore/errors'
|
|
import { Authz, ensureError, throwUncoveredError } from '@speckle/shared'
|
|
import VError from 'verror'
|
|
|
|
/**
|
|
* Resolve cause correctly depending on whether its a VError or basic Error
|
|
* object
|
|
*/
|
|
export function getCause(e: Error) {
|
|
if (e instanceof VError) {
|
|
return VError.cause(e)
|
|
} else {
|
|
const unknownCause = e.cause
|
|
return unknownCause ? ensureError(e.cause) : null
|
|
}
|
|
}
|
|
|
|
export { ensureError }
|
|
|
|
/**
|
|
* Global mapping for mapping any kind of auth error to a server thrown error
|
|
*/
|
|
export const mapAuthToServerError = (e: Authz.AllAuthErrors): BaseError => {
|
|
switch (e.code) {
|
|
case Authz.ProjectNotFoundError.code:
|
|
return new StreamNotFoundError(e.message)
|
|
case Authz.ProjectNoAccessError.code:
|
|
case Authz.WorkspaceNoAccessError.code:
|
|
case Authz.WorkspaceNotEnoughPermissionsError.code:
|
|
case Authz.WorkspaceReadOnlyError.code:
|
|
case Authz.WorkspaceLimitsReachedError.code:
|
|
case Authz.WorkspaceNoEditorSeatError.code:
|
|
case Authz.WorkspaceProjectMoveInvalidError.code:
|
|
case Authz.CommentNoAccessError.code:
|
|
case Authz.ProjectNotEnoughPermissionsError.code:
|
|
case Authz.WorkspacePlanNoFeatureAccessError.code:
|
|
case Authz.EligibleForExclusiveWorkspaceError.code:
|
|
return new ForbiddenError(e.message)
|
|
case Authz.WorkspaceSsoSessionNoAccessError.code:
|
|
throw new SsoSessionMissingOrExpiredError(e.message, {
|
|
info: {
|
|
workspaceSlug: e.payload.workspaceSlug
|
|
}
|
|
})
|
|
case Authz.ServerNoAccessError.code:
|
|
case Authz.ServerNoSessionError.code:
|
|
case Authz.ServerNotEnoughPermissionsError.code:
|
|
return new ForbiddenError(e.message)
|
|
case Authz.WorkspacesNotEnabledError.code:
|
|
return new WorkspacesModuleDisabledError()
|
|
case Authz.ProjectLastOwnerError.code:
|
|
case Authz.ReservedModelNotDeletableError.code:
|
|
return new BadRequestError(e.message)
|
|
case Authz.CommentNotFoundError.code:
|
|
case Authz.ModelNotFoundError.code:
|
|
case Authz.VersionNotFoundError.code:
|
|
return new NotFoundError(e.message)
|
|
case Authz.PersonalProjectsLimitedError.code:
|
|
return new BadRequestError(e.message)
|
|
default:
|
|
throwUncoveredError(e)
|
|
}
|
|
}
|
|
|
|
export const throwIfAuthNotOk = (result: Authz.AuthPolicyResult) => {
|
|
if (result.isOk) return
|
|
throw mapAuthToServerError(result.error)
|
|
}
|