Skip to content

Commit

Permalink
fix errors b00tc4mp#140
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonzalo committed Jul 15, 2024
1 parent 76d3b73 commit 772b01b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 36 deletions.
48 changes: 31 additions & 17 deletions staff/adrian-martin/socialcode/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express'
import logic from './logic/index.js'
import cors from 'cors'
import jwt from './utils/jsonwebtoken-promised.js'
import error, { SystemError } from 'com/error.js'
import { ContentError, CredentialError, DuplicityError, MatchError, NotFoundError, SystemError } from 'com/error.js'
import 'dotenv/config'
import mongoose from 'mongoose'

Expand All @@ -22,15 +22,36 @@ mongoose.connect(MONGODB_URL)

const jsonBodyParser = express.json({ strict: true, type: 'application/json' })

function handleErrorResponse(error, res) {

let status = 500

if (error instanceof DuplicityError)
status = 409
else if (error instanceof ContentError)
status = 400
else if (error instanceof MatchError)
status = 412
else if (error instanceof CredentialError)
status = 401
else if (error instanceof NotFoundError)
status = 404

res.status(status).json({ error: error.constructor.name, message: error.message })
}

api.post('/users', jsonBodyParser, (req, res) => {
const { name, surname, email, username, password, passwordRepeat } = req.body

try {
logic.registerUser(name, surname, email, username, password, passwordRepeat)
.then(() => res.status(201).send())
.catch(error => res.status(500).json({ error: error.constructor.name, message: error.message }))
.catch(error => {
handleErrorResponse(error, res)

})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
handleErrorResponse(error, res)
}
})

Expand All @@ -47,11 +68,11 @@ mongoose.connect(MONGODB_URL)

res.json(token)
})
.catch(error => res.status(500).json({ error: error.constructor.name, message: error.message }))
.catch(error => handleErrorResponse(new SystemError(error.message), res))
})
.catch(error => res.status(500).json({ error: error.constructor.name, message: error.message }))
.catch(error => handleErrorResponse(error, res))
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
handleErrorResponse(error, res)
}
})

Expand All @@ -69,21 +90,14 @@ mongoose.connect(MONGODB_URL)
try {
logic.getUserName(userId, targetUserId)
.then(name => res.json(name))
.catch(error => res.status(500).json({ error: error.constructor.name, message: error.message }))
.catch(error => handleErrorResponse(error, res))
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
handleErrorResponse(error, res)
}
})
.catch(error => {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })

return
})
.catch(error => handleErrorResponse(new CredentialError(error.message), res))
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
handleErrorResponse(error, res)
}
})

Expand Down
10 changes: 5 additions & 5 deletions staff/adrian-martin/socialcode/api/logic/authenticateUser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from '../data/index.js'
import { MatchError, SystemError } from 'com/error.js'
import { CredentialError, SystemError } from 'com/error.js'
import validate from 'com/validate.js'
import bcrypt from 'bcryptjs'

Expand All @@ -11,17 +11,17 @@ const authenticateUser = (username, password) => {
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) {
throw new MatchError('user not found')
throw new CredentialError('user not found')
}


return bcrypt.compare(password, user.password)
.catch(error => { throw new SystemError(error.message) })
.then(match => {
if (!match)
throw new MatchError('wrong password')
if (!match)
throw new CredentialError('wrong password')

return user._id.toString()
return user._id.toString()
})
})
}
Expand Down
4 changes: 2 additions & 2 deletions staff/adrian-martin/socialcode/api/logic/getUserName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from '../data/index.js'
import { MatchError } from 'com/error.js'
import { MatchError, NotFoundError } from 'com/error.js'
import validate from 'com/validate.js'


Expand All @@ -18,7 +18,7 @@ const getUserName = (userId, targetUserId) => {
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) {
throw new MatchError('targetUsername not found')
throw new NotFoundError('targetUsername not found')
}
return user.name
})
Expand Down
44 changes: 32 additions & 12 deletions staff/adrian-martin/socialcode/com/error.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
class ContentError extends Error{
constructor(message){
class ContentError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

class MatchError extends Error{
constructor(message){
class MatchError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

class DuplicityError extends Error{
constructor(message){
class DuplicityError extends Error {
constructor(message) {
super(message)


this.name = this.constructor.name
}
}

class SystemError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

class SystemError extends Error{
constructor(message){
class CredentialError extends Error {
constructor(message) {
super(message)


this.name = this.constructor.name
}
}

class NotFoundError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}
Expand All @@ -34,14 +50,18 @@ export {
ContentError,
MatchError,
DuplicityError,
SystemError
SystemError,
CredentialError,
NotFoundError
}

const error = {
ContentError,
MatchError,
DuplicityError,
SystemError
SystemError,
CredentialError,
NotFoundError
}

export default error

0 comments on commit 772b01b

Please sign in to comment.