11import * as userRepo from "./../repositories/user.repository.ts" ;
2- import { httpErrors , HttpError } from "https://deno.land/x/[email protected] /mod.ts" ; 2+ import { httpErrors } from "https://deno.land/x/[email protected] /mod.ts" ; 33
44/**
55 * get user by id
@@ -26,26 +26,43 @@ export const getUsers = async () => {
2626 */
2727export const createUser = async ( userData : any ) => {
2828 // todo: validation
29- // todo: catch db error
30- const user = await userRepo . createUser ( userData ) ;
31- return user ;
29+ try {
30+ const user = await userRepo . createUser ( userData ) ;
31+ return user ;
32+ } catch ( err ) {
33+ const { message } = err ;
34+ if ( message . match ( "email_unique" ) ) {
35+ throw new httpErrors . BadRequest (
36+ `Already user exists with email ${ userData . email } ` ,
37+ ) ;
38+ }
39+ throw err ;
40+ }
3241} ;
3342
3443/**
3544 * update user
3645 */
3746export const updateUser = async ( id : number , userData : any ) => {
3847 // todo: validation
39- // todo: catch db error
40- const result = await userRepo . updateUser ( id , userData ) ;
41- if ( result [ "affectedRows" ] ) {
42- const user = await userRepo . getUserById ( id ) ;
43- if ( user ) {
44- return user ;
48+ try {
49+ const result = await userRepo . updateUser ( id , userData ) ;
50+ if ( result [ "affectedRows" ] ) {
51+ const user = await userRepo . getUserById ( id ) ;
52+ if ( user ) {
53+ return user ;
54+ }
55+ }
56+ throw new httpErrors . NotFound ( "User not found" ) ;
57+ } catch ( err ) {
58+ const { message } = err ;
59+ if ( message . match ( "email_unique" ) ) {
60+ throw new httpErrors . BadRequest (
61+ `Already user exists with email ${ userData . email } ` ,
62+ ) ;
4563 }
64+ throw err ;
4665 }
47-
48- throw new httpErrors . NotFound ( "User not found" ) ;
4966} ;
5067
5168/**
0 commit comments