Skip to content

Commit

Permalink
fix: send 400 if transmitter not found
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed Jul 8, 2024
1 parent 3c118f9 commit 30d9ddc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions api/src/errors/BadRequestError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default class BadRequestError extends CustomError {
private readonly _logging: boolean;
private readonly _context: { [key: string]: any };

constructor(params?: {code?: number, message?: string, logging?: boolean, context?: { [key: string]: any }}) {
constructor(params?: { code?: number, message?: string, logging?: boolean, context?: { [key: string]: any } }) {
const { code, message, logging } = params || {};

super(message || "Bad request");
super(message || "Bad request", 400);
this._code = code || BadRequestError._statusCode;
this._logging = logging || false;
this._context = params?.context || {};
Expand Down
14 changes: 11 additions & 3 deletions api/src/errors/CustomError.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { GraphQLError } from "graphql";

export type CustomErrorContent = {
message: string,
context?: { [key: string]: any }
};

export abstract class CustomError extends Error {
export abstract class CustomError extends GraphQLError {
abstract readonly statusCode: number;
abstract readonly errors: CustomErrorContent[];
abstract readonly logging: boolean;

constructor(message: string) {
super(message);
constructor(message: string, status: number = 200) {
super(message, {
extensions: {
http: {
status
}
}
});

// Only because we are extending a built in class
Object.setPrototypeOf(this, CustomError.prototype);
Expand Down
2 changes: 1 addition & 1 deletion api/src/errors/NotFoundError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class NotFoundError extends CustomError {
constructor(params?: { code?: number, message?: string, logging?: boolean, context?: { [key: string]: any } }) {
const { code, message, logging } = params || {};

super(message || "Not Found");
super(message || "Not Found", 404);
this._code = code || NotFoundError._statusCode;
this._logging = logging || false;
this._context = params?.context || {};
Expand Down
2 changes: 2 additions & 0 deletions api/src/graphql/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ function loggerPlugin(): ApolloServerPlugin<Context> {
const createApolloIndexServer = (): ApolloServer<IndexContext> => new ApolloServer<IndexContext>({
schema: indexSchema,
introspection: true,
status400ForVariableCoercionErrors: true,
plugins: [loggerPlugin()],
});

const createApolloDriveServer = (): ApolloServer<DriveContext> => new ApolloServer<DriveContext>({
schema: driveSchema,
introspection: true,
status400ForVariableCoercionErrors: true,
plugins: [loggerPlugin()],
});

Expand Down
9 changes: 8 additions & 1 deletion api/src/modules/document-drive/drive-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from 'document-model-libs/document-drive';
import DocumentDriveError from '../../errors/DocumentDriveError';
import NotFoundError from '../../errors/NotFoundError';
import { GraphQLError } from 'graphql';

const logger = getChildLogger({ msgPrefix: 'Drive Resolver' });

Expand Down Expand Up @@ -285,7 +286,13 @@ export const syncType = objectType({
}));
} catch (e) {
if ((e as Error).message?.match(/Transmitter .+ not found/)) {
throw new NotFoundError({ message: 'Transmitter not found' });
throw new GraphQLError((e as Error).message ?? 'Transmitter not found', {
extensions: {
http: {
status: 400,
},
},
});
} else {
logger.error(e);
throw new Error('Failed to fetch strands');
Expand Down
1 change: 0 additions & 1 deletion api/src/modules/document/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
listenerId: string,
since?: string,
): Promise<StrandUpdate[]> => {

const transmitter = await getTransmitter(driveId, listenerId);
if (transmitter.getStrands) {
const result = await transmitter.getStrands(since || undefined);
Expand Down

0 comments on commit 30d9ddc

Please sign in to comment.