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

Custom Error #5

Open
antoniopaolillo opened this issue Apr 28, 2020 · 8 comments
Open

Custom Error #5

antoniopaolillo opened this issue Apr 28, 2020 · 8 comments

Comments

@antoniopaolillo
Copy link

It would be interesting if there was how to customize the error returned

@roziscoding
Copy link
Member

roziscoding commented Apr 28, 2020

True.

The proposed API is:

const { validate } = require('@expresso/validator')

function getCustomError (ajvErrors) {
    return new MyCustomError(`Errors: ${ajvErrors.join('\n')}`)
}

module.exports = [
    validate({
        type: 'object',
        properties: {
            email: {
                type: 'string',
                format: 'email'
            },
            password: {
                type: 'string',
                minLength: 6
            },
            firstName: {
                type: 'string',
                minLength: 3,
                // pattern: '/^[a-z]+$/i'
            },
            phone: {
                type: 'string',
                // pattern: '/^\\+\\d{2}\\s\\(\\d{2}\\)\\s\\d{4,5}-?\\d{4}$/g'
            }
        },
        required: ['email', 'password', 'firstName', 'phone']
    }, { getCustomError }),
    function (req, res) {
        // regular route handling
    }
]

@roziscoding
Copy link
Member

@khaosdoctor what do you think about this API?

@khaosdoctor
Copy link
Member

I think it should be simpler:

// ErrorFile.ts
export class MyError {
	constructor (message) { super(message) }
}

// RouteFile.ts
import { MyError } from './ErrorFile'

module.exports = [
    validate({
        type: 'object',
        properties: {
            email: {
                type: 'string',
                format: 'email'
            },
            password: {
                type: 'string',
                minLength: 6
            },
            firstName: {
                type: 'string',
                minLength: 3,
                // pattern: '/^[a-z]+$/i'
            },
            phone: {
                type: 'string',
                // pattern: '/^\\+\\d{2}\\s\\(\\d{2}\\)\\s\\d{4,5}-?\\d{4}$/g'
            }
        },
        required: ['email', 'password', 'firstName', 'phone']
    }, MyError),
    function (req, res) {
        // regular route handling
    }
]

Then inside the validator we wrap the error and create a joined message but returning user error

@roziscoding
Copy link
Member

roziscoding commented Apr 30, 2020

What if the user doesn't want to use a class as an error?
We ourselves don't use a class for this. We create a Boom error.

Or what if users want to use a different class based on, say, environment?

Using a function seems more flexible, and doesn't add that much complexity on our side.
Also, the second parameter is already an options object, so getCustomError would be just another property on that object

@khaosdoctor
Copy link
Member

But a function can be used as a constructor just like a class. You can pass either a function or a class

@roziscoding
Copy link
Member

Well, then it doesn't need to be specified as a class 🤔
We'll just call the function and use whatever it returns as the error

@roziscoding
Copy link
Member

@khaosdoctor can I go with the function? Thinking about implementing it tonight

@khaosdoctor
Copy link
Member

go on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants