Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
update more error code
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed Feb 10, 2024
1 parent 3a79b1c commit e53c506
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 35 deletions.
44 changes: 27 additions & 17 deletions lib/dtos/response/base_response_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BaseResponseData {
const BaseResponseData({
required this.success,
this.result,
this.errorCode,
this.message = kSuccessResponseMessage,
});

Expand All @@ -34,29 +35,32 @@ class BaseResponseData {
);
}

factory BaseResponseData.failed([String? message, dynamic stackTrace]) {
factory BaseResponseData.failed({String? errorCode, String? message}) {
return BaseResponseData(
success: false,
errorCode: errorCode,
message: message ?? kFailedResponseMessage,
result: stackTrace,
);
}

final bool success;
final String? errorCode;
final String message;
final dynamic result;

Map<String, dynamic> toJson() => _$BaseResponseDataToJson(this);

BaseResponseData copyWith({
bool? success,
String? errorCode,
String? message,
dynamic data,
dynamic result,
}) {
return BaseResponseData(
success: success ?? this.success,
errorCode: errorCode ?? this.errorCode,
message: message ?? this.message,
result: data ?? result,
result: result ?? this.result,
);
}
}
Expand All @@ -80,50 +84,56 @@ class CreatedResponse extends Response {
}

class NotFoundResponse extends Response {
NotFoundResponse([String? message])
NotFoundResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.notFound,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class ConflictResponse extends Response {
ConflictResponse([String? message])
ConflictResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.conflict,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class UnauthorizedResponse extends Response {
UnauthorizedResponse([String? message])
UnauthorizedResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.unauthorized,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class BadRequestResponse extends Response {
BadRequestResponse([String? message])
BadRequestResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.badRequest,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class ForbiddenResponse extends Response {
ForbiddenResponse([String? message])
ForbiddenResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.forbidden,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class ServerErrorResponse extends Response {
ServerErrorResponse([String? message, dynamic stackTrace])
class InternalServerErrorResponse extends Response {
InternalServerErrorResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.internalServerError,
body: BaseResponseData.failed(message, stackTrace).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

Expand Down
3 changes: 2 additions & 1 deletion routes/api/auth/login/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ Future<Response> _onLoginPostRequest(RequestContext context) async {
: OkResponse(
LoginResponse(id: user.id, token: createJwt(user.id)).toJson(),
);
}).onError((e, _) => ServerErrorResponse(ErrorMessageCode.unknownError));
}).onError(
(e, _) => InternalServerErrorResponse(ErrorMessageCode.unknownError));
}
2 changes: 1 addition & 1 deletion routes/api/auth/register/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ Future<Response> _onRegisterPostRequest(RequestContext context) async {
)
.then<Response>((_) => CreatedResponse())
.onError(
(e, st) => ServerErrorResponse(ErrorMessageCode.unknownError, st),
(e, st) => InternalServerErrorResponse(ErrorMessageCode.unknownError),
);
}
6 changes: 3 additions & 3 deletions routes/api/blogs/[id]/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Future<Response> _onBlogsGetRequest(RequestContext context, String id) async {
.toJson(),
);
} catch (e) {
return ServerErrorResponse(e.toString());
return InternalServerErrorResponse(e.toString());
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ Future<Response> _onBlogsPatchRequest(RequestContext context, String id) async {
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
} catch (e) {
return ServerErrorResponse(e.toString());
return InternalServerErrorResponse(e.toString());
}
}

Expand All @@ -102,6 +102,6 @@ Future<Response> _onBlogsDeleteRequest(
await db.blogs.deleteOne(id);
return OkResponse();
} catch (e) {
return ServerErrorResponse(e.toString());
return InternalServerErrorResponse(e.toString());
}
}
4 changes: 2 additions & 2 deletions routes/api/blogs/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Future<Response> _onBlogsGetRequest(RequestContext context) async {
final blogs = results.map(GetBlogResponse.fromView);
return OkResponse(blogs.map((e) => e.toJson()).toList());
} catch (e) {
return ServerErrorResponse(e.toString());
return InternalServerErrorResponse(e.toString());
}
}

Expand Down Expand Up @@ -69,6 +69,6 @@ Future<Response> _onBlogsPostRequest(RequestContext context) async {
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
} catch (e) {
return ServerErrorResponse(e.toString());
return InternalServerErrorResponse(e.toString());
}
}
6 changes: 3 additions & 3 deletions routes/api/favorites/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Future<Response> _onFavoritesGetRequest(RequestContext context) {
)
.then((r) => r.map(GetUserFavoriteBlogResponse.fromView).toList())
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
}

Future<Response> _onFavoritesPostRequest(RequestContext context) async {
Expand Down Expand Up @@ -72,7 +72,7 @@ Future<Response> _onFavoritesPostRequest(RequestContext context) async {
),
)
.then<Response>((_) => OkResponse())
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
}
return db
.query(
Expand All @@ -81,7 +81,7 @@ Future<Response> _onFavoritesPostRequest(RequestContext context) async {
{'blogId': request.blogId, 'userId': userView.id},
)
.then<Response>((_) => OkResponse())
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
}
Expand Down
6 changes: 3 additions & 3 deletions routes/api/followings/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Future<Response> _onFollowingPost(RequestContext context) async {
{'followingId': userView.id, 'userId': userView.id},
)
.then<Response>((_) => OkResponse())
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
}
return db.followingFollowers
.insertOne(
Expand All @@ -58,10 +58,10 @@ Future<Response> _onFollowingPost(RequestContext context) async {
),
)
.then<Response>((_) => OkResponse())
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
} catch (e) {
return ServerErrorResponse(e.toString());
return InternalServerErrorResponse(e.toString());
}
}
2 changes: 1 addition & 1 deletion routes/api/users/[id]/blogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ Future<Response> _onUsersByIdBlogsGet(RequestContext context, String id) {
.queryBlogs(QueryParams(where: 'creator_id=@id', values: {'id': id}))
.then((views) => views.map(GetUserBlogResponse.fromView))
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
}
2 changes: 1 addition & 1 deletion routes/api/users/[id]/followers/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Future<Response> _onFollowersByIdGetRequest(
)
.then((result) => result.map(GetUserFollowerResponse.fromView))
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
}
2 changes: 1 addition & 1 deletion routes/api/users/[id]/followings/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Future<Response> _onFollowingsByIdGetRequest(
)
.then((result) => result.map(GetUserFollowingResponse.fromView))
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
}
4 changes: 2 additions & 2 deletions routes/api/users/[id]/profiles/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Future<Response> _onUserByIdGetRequest(
).toJson(),
),
)
.catchError((_) => ServerErrorResponse());
.catchError((_) => InternalServerErrorResponse());
}

Future<Response> _onUserByIdPatchRequest(
Expand Down Expand Up @@ -73,7 +73,7 @@ Future<Response> _onUserByIdPatchRequest(
),
)
.then<Response>((_) => OkResponse())
.onError((e, _) => ServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()));
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
}
Expand Down

0 comments on commit e53c506

Please sign in to comment.