Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some error is not DriverError. Make all error from database level become DriverError or kind of type ! #4897

Closed
dungkaka opened this issue Nov 5, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@dungkaka
Copy link

dungkaka commented Nov 5, 2023

I work on NestJs and build a FilterException to handle Mikroorm Error separate.

// postgresException.filter.ts
import { DriverException } from "@mikro-orm/core";

@Catch(DriverException)
export class PostgresExceptionFilter implements ExceptionFilter {
    constructor(private readonly httpAdapterHost?: HttpAdapterHost) {}

    catch(exception: DriverException, host: ArgumentsHost): void {
        const { httpAdapter } = this.httpAdapterHost;
        const ctx = host.switchToHttp();

        const dataResponse: IErrorResponseData = {
            status: false,
            errorCode: HttpStatus.INTERNAL_SERVER_ERROR,
            message: exception["detail"] || exception.message,
            metaData: {
                dbCode: exception.code
            }
        };

        (process.env.NODE_ENV == "development" || process.env.NODE_ENV == "local") &&
            console.log("LOG_POSTGRES_EXCEPTION", exception);

        httpAdapter.reply(ctx.getResponse(), dataResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

It works on almost cases. When I upsert, it not raise DriverError, just Error:

 await this.em.upsertMany(Enroll, enrollmentUpserts, {
            onConflictFields: ["user", "course"],
            onConflictAction: "ignore"
        });

error: insert into "course"."enroll" ("course_id", "register_at", "user_id") values (32, '2023-11-05T18:16:27.381Z', 1), (32, '2023-11-05T18:16:27.381Z', 2), (32, '2023-11-05T18:16:27.381Z', 4), (32, '2023-11-05T18:16:27.381Z', 5), (32, '2023-11-05T18:16:27.381Z', 1001) on conflict ("user_id", "course_id") do nothing returning "register_at", "expired_at" - insert or update on table "enroll" violates foreign key constraint "enroll_user_id_foreign"

It would be

ForeignKeyConstraintViolationException:  insert into "course"."enroll" ("course_id", "register_at", "user_id") values (32, '2023-11-05T18:16:27.381Z', 1), (32, '2023-11-05T18:16:27.381Z', 2), (32, '2023-11-05T18:16:27.381Z', 4), (32, '2023-11-05T18:16:27.381Z', 5), (32, '2023-11-05T18:16:27.381Z', 1001) on conflict ("user_id", "course_id") do nothing returning "register_at", "expired_at" - insert or update on table "enroll" violates foreign key constraint "enroll_user_id_foreign"
@dungkaka dungkaka added the enhancement New feature or request label Nov 5, 2023
@B4nan B4nan closed this as completed in 615f223 Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant