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

feat: Setup local user register & handle exception error to logger #6

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added log/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^6.1.4",
"@nestjs/typeorm": "^9.0.1",
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dotenv": "^16.0.3",
"morgan": "^1.10.0",
"mysql2": "^2.3.3",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
Expand Down
12 changes: 11 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { APP_FILTER } from "@nestjs/core";
import { TypeOrmModule } from "@nestjs/typeorm";

import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { dataSourceOptions } from "./config/data-source";
import { validate } from "./config/env.validation";
import { AllExceptionsFilter } from "./filters/all-exception.filter";
import { UsersModule } from "./users/users.module";

@Module({
imports: [
ConfigModule.forRoot({
validate,
}),
TypeOrmModule.forRoot(dataSourceOptions),
UsersModule,
],
controllers: [AppController],
providers: [AppService],
providers: [
AppService,
{
provide: APP_FILTER,
useClass: AllExceptionsFilter,
},
],
})
export class AppModule {}
37 changes: 37 additions & 0 deletions src/database/migrations/1679539757893-UserInitMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class UserInitMigration1679539757893 implements MigrationInterface {
name = "UserInitMigration1679539757893";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE \`users\`
(
\`id\` int NOT NULL AUTO_INCREMENT,
\`email\` varchar(255) NOT NULL,
\`name\` varchar(255) NOT NULL,
\`account\` varchar(255) NOT NULL,
\`password\` varchar(255) NOT NULL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bcrypt hashed password should only need 60 char length

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change in commit
0f4a61d

\`createAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
\`updateAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
UNIQUE INDEX \`IDX_97672ac88f789774dd47f7c8be\` (\`email\`),
UNIQUE INDEX \`IDX_51b8b26ac168fbe7d6f5653e6c\` (\`name\`),
UNIQUE INDEX \`IDX_dd44b05034165835d6dcc18d68\` (\`account\`),
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX \`IDX_dd44b05034165835d6dcc18d68\` ON \`users\``,
);
await queryRunner.query(
`DROP INDEX \`IDX_51b8b26ac168fbe7d6f5653e6c\` ON \`users\``,
);
await queryRunner.query(
`DROP INDEX \`IDX_97672ac88f789774dd47f7c8be\` ON \`users\``,
);
await queryRunner.query(`DROP TABLE \`users\``);
}
}
29 changes: 29 additions & 0 deletions src/database/migrations/1681641687260-AdjustUserFieldMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AdjustUserFieldMigration1681641687260
implements MigrationInterface
{
name = "AdjustUserFieldMigration1681641687260";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX \`IDX_51b8b26ac168fbe7d6f5653e6c\` ON \`users\``,
);
await queryRunner.query(
`ALTER TABLE \`users\` MODIFY \`password\` varchar(60) NOT NULL`,
);
await queryRunner.query(`ALTER TABLE \`users\` DROP COLUMN \`updateAt\``);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`users\` ADD \`updateAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`users\` MODIFY \`password\` varchar(255) NOT NULL`,
);
await queryRunner.query(
`CREATE UNIQUE INDEX \`IDX_51b8b26ac168fbe7d6f5653e6c\` ON \`users\` (\`name\`)`,
);
}
}
92 changes: 92 additions & 0 deletions src/filters/all-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
} from "@nestjs/common";
import { Request, Response } from "express";
import * as fs from "fs";
import { QueryFailedError } from "typeorm";

import { CustomHttpExceptionResponse } from "./models/http-exception-response.interface";

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();

let status: HttpStatus;
let tinyErrorMessage: string;
let fullErrorMessage: string;
let errorMessage: string;
if (exception instanceof HttpException) {
status = exception.getStatus();
const errorResponse = exception.getResponse();
errorMessage = errorResponse["message"];
} else if (exception instanceof TypeError) {
status = HttpStatus.BAD_REQUEST;
errorMessage = exception.message
.substring(exception.message.indexOf("\n\n\n") + 1)
.trim();
} else if (exception instanceof QueryFailedError) {
status = HttpStatus.INTERNAL_SERVER_ERROR;
tinyErrorMessage = exception.message
.substring(exception.message.indexOf("\n\n\n") + 1)
.trim();
fullErrorMessage = exception["sql"];
errorMessage = "Critical internal server error occurred!";
} else {
status = HttpStatus.INTERNAL_SERVER_ERROR;
errorMessage = "Critical internal server error occurred!";
fullErrorMessage = JSON.stringify(exception);
}
const errorResponse = this.getErrorResponse(status, errorMessage, request);
const errorLog = this.getErrorLog(
tinyErrorMessage,
fullErrorMessage,
errorResponse,
request,
exception,
);
this.writeErrorLogToFile(errorLog);
response.status(status).json(errorResponse);
}

private getErrorResponse = (
status: HttpStatus,
errorMessage: string,
request: Request,
): CustomHttpExceptionResponse => ({
statusCode: status,
error: errorMessage,
path: request.url,
method: request.method,
timeStamp: new Date(),
});

private getErrorLog = (
tinyErrorMessage: string,
fullErrorMessage: string,
errorResponse: CustomHttpExceptionResponse,
request: Request,
exception: unknown,
): string => {
const { statusCode, error } = errorResponse;
const { method, url } = request;
const errorLog = `Response Code: ${statusCode} - Method: ${method} - URL: ${url}
${JSON.stringify(errorResponse)}
${tinyErrorMessage != undefined ? tinyErrorMessage : ""}
${fullErrorMessage != undefined ? fullErrorMessage : ""}
${exception instanceof HttpException ? exception.stack : error}\n\n`;
return errorLog;
};

private writeErrorLogToFile = (errorLog: string): void => {
fs.appendFile("./log/error.log", errorLog, "utf8", err => {
if (err) throw err;
});
};
}
7 changes: 7 additions & 0 deletions src/filters/models/http-exception-response.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface CustomHttpExceptionResponse {
statusCode: number;
error: string;
path: string;
method: string;
timeStamp: Date;
}
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { INestApplication } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import * as fs from "fs";
import * as morgan from "morgan";

import { AppModule } from "./app.module";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(morgan("default", { stream: logStream }));
Copy link
Member

@moontai0724 moontai0724 Mar 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not described in PR content, should consider to add description about this

setupSwagger(app);
await app.listen(3000);
}

const logStream = fs.createWriteStream("./log/access.log", {
flags: "a", // append
});

function setupSwagger(app: INestApplication) {
const builder = new DocumentBuilder();
const config = builder
Expand Down
62 changes: 62 additions & 0 deletions src/users/dto/create-user.dto.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xxxx is required field can use xxxx is required instead.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ApiProperty, PickType } from "@nestjs/swagger";
import { IsEmail, IsNotEmpty, Length } from "class-validator";

export class CreateUserDto {
@ApiProperty({
description: "User email",
example: "[email protected]",
})
@IsEmail({}, { message: "email must be in mailbox format." })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use email format does not correct just fine

@IsNotEmpty({
message: "email is required field.",
})
public readonly email: string;

@ApiProperty({
description: "User showname",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use User display name may better

example: "showname",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to use a name or something

})
@IsNotEmpty({
message: "name is required field.",
})
public readonly name: string;

@ApiProperty({
description: "User account",
example: "account",
})
@IsNotEmpty({
message: "account is required field.",
})
public readonly account: string;
Comment on lines +24 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may need a length limit and character limit like only accept numbers, alphabets and underscores.


@ApiProperty({
description: "User Password",
example: "Password@123",
})
@IsNotEmpty({
message: "password is required field.",
})
@Length(8, 24, {
message: "password's length must be between 8-24 characters.",
})
Comment on lines +40 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why limit password length? I would not limit this, or only limit minimum length

public readonly password: string;

@ApiProperty({
description: "check password again",
example: "Password@123",
})
@IsNotEmpty({
message: "confirm is required field.",
})
@Length(8, 24, {
message: "confirm's length must be between 8-24 characters.",
})
public readonly confirm: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think backend can ignore password confirm field, this can be done by frontend

}
export class CreateUserParam extends PickType(CreateUserDto, [
"name",
"email",
"account",
"password",
] as const) {}
30 changes: 30 additions & 0 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
BaseEntity,
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
} from "typeorm";

@Entity({ name: "users" })
export class UserEntity extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column({ unique: true })
email: string;

@Column()
name: string;

@Column({ unique: true })
account: string;

@Column({
length: 60,
})
password: string;

@CreateDateColumn()
createAt: Date;
}
39 changes: 39 additions & 0 deletions src/users/exceptions/create-conflict-error.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ApiProperty } from "@nestjs/swagger";

export class CreateConflictUserError {
@ApiProperty({
type: "number",
description: "HTTP StatusCode",
example: "409",
})
public readonly StatusCode: number;

@ApiProperty({
type: "array",
description: "Error Message",
items: {
properties: {
email: {
description: "email has been registered. \n",
type: "string",
},
name: {
description: "name has been registered. \n",
type: "string",
},
account: {
description: "account has been registered. \n",
type: "string",
},
password: {
description:
"Confirm and password do not match, please try again. \n",

type: "string",
},
},
},
example: "email has been registered.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be an array of message

})
public readonly error: string[];
}
Loading