2022-05-18 07:26:57 +00:00
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
|
import type { SchemaError } from '../__generated__/types';
|
|
|
|
|
|
|
|
|
|
export function toSchemaError(error: unknown): SchemaError {
|
|
|
|
|
if (isGraphQLError(error)) {
|
|
|
|
|
return {
|
|
|
|
|
message: error.message,
|
2022-12-28 20:04:17 +00:00
|
|
|
path: error.path?.map(i => (typeof i === 'number' ? String(i) : i)),
|
2022-05-18 07:26:57 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
message: error.message,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
message: error as string,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isGraphQLError(error: unknown): error is GraphQLError {
|
|
|
|
|
return error instanceof GraphQLError;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:48:01 +00:00
|
|
|
export const HiveError = GraphQLError;
|
2022-05-18 07:26:57 +00:00
|
|
|
|
|
|
|
|
export class AccessError extends HiveError {
|
2026-02-05 15:54:13 +00:00
|
|
|
constructor(reason: string, code: string = 'UNAUTHORISED', extensions?: Record<string, unknown>) {
|
2024-03-26 12:42:56 +00:00
|
|
|
super(`No access (reason: "${reason}")`, {
|
|
|
|
|
extensions: {
|
|
|
|
|
code,
|
2026-02-05 15:54:13 +00:00
|
|
|
...extensions,
|
2024-03-26 12:42:56 +00:00
|
|
|
},
|
|
|
|
|
});
|
2022-05-18 07:26:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-06 07:47:47 +00:00
|
|
|
|
2026-02-05 15:54:13 +00:00
|
|
|
export class OIDCRequiredError extends AccessError {
|
|
|
|
|
constructor(
|
|
|
|
|
organizationSlug: string,
|
|
|
|
|
oidcIntegrationId: string,
|
|
|
|
|
reason: string = 'This action requires OIDC authentication to proceed.',
|
|
|
|
|
) {
|
|
|
|
|
super(reason, 'NEEDS_OIDC', { organizationSlug, oidcIntegrationId });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-06 07:47:47 +00:00
|
|
|
/**
|
|
|
|
|
* This error indicates that the user forgot to provide a target reference input
|
|
|
|
|
* when using a organization access token.
|
|
|
|
|
*/
|
|
|
|
|
export class MissingTargetError extends HiveError {
|
|
|
|
|
constructor(reason = 'No target was provided.', code: string = 'ERR_MISSING_TARGET') {
|
|
|
|
|
super(reason, {
|
|
|
|
|
extensions: {
|
|
|
|
|
code,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|