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: Simulate nodejs-level errors ? #365

Open
GerkinDev opened this issue Mar 11, 2023 · 3 comments · May be fixed by #384
Open

Feat: Simulate nodejs-level errors ? #365

GerkinDev opened this issue Mar 11, 2023 · 3 comments · May be fixed by #384

Comments

@GerkinDev
Copy link

Hi,

For testing purpose, I wanted to check the behavior of my application when receiving errors ECONNRESET, ENOTFOUND, ECONNREFUSED or EHOSTUNREACH, and the only solution I found (after quite some time of research) was to do the following:

axiosMock.onGet('/api/v1/version').reply(() => {
    throw Object.assign(new Error('connect ECONNREFUSED test:1234'), {
        errno: os.constants.errno.ECONNREFUSED,
        syscall: 'connect',
        address: 'test',
        port: 1234,
        code: 'ECONNREFUSED'
    })
});

While it seems to be working as expected, it's rather verbose, and would be super useful to have helpers to throw such errors.
I've made a runkit with a few samples of those errors:
https://runkit.com/embed/12bax0k5843m

Proposal:

Add an optional parameter to networkError, which would be the code of the error to throw. For what I've seen, errors could look like this:

const NET_ERRORS = {
  ECONNRESET: {
    code: 'ECONNRESET',
    message: 'socket hang up',
    errno: os.constants.errno.ECONNRESET,
  },
  ENOTFOUND: {
    code: 'ENOTFOUND',
    message: 'getaddrinfo ENOTFOUND http://test:1234',
    syscall: 'getaddrinfo',
    hostname: 'http://test',
    errno: os.constants.errno.ENOTFOUND,
  },
  ECONNREFUSED: {
    code: 'ECONNREFUSED',
    message: 'connect ECONNREFUSED 1.2.3.4:1234',
    errno: os.constants.errno.ECONNREFUSED,
    syscall: 'connect',
    address: '1.2.3.4',
    port: 1234,
  },
  EHOSTUNREACH: {
    code: 'EHOSTUNREACH',
    message: 'connect EHOSTUNREACH 1.2.3.4:1234',
    errno: os.constants.errno.EHOSTUNREACH,
    syscall: 'connect',
    address: '1.2.3.4',
    port: 1234,
  },
};

I'm OK to PR

@mnikolaus
Copy link

mnikolaus commented Apr 18, 2024

@GerkinDev can you still prepare the PR, this looks like its really needs to be added

@GerkinDev
Copy link
Author

GerkinDev commented Apr 18, 2024

I'll do that tomorrow, and publish a built git branch you'll be able to install directly

GerkinDev added a commit to craft-ai/fork-axios-mock-adapter that referenced this issue Apr 19, 2024
@GerkinDev GerkinDev linked a pull request Apr 19, 2024 that will close this issue
@GerkinDev
Copy link
Author

Done. @mnikolaus you should be able to install it using npm install git+https://github.com/craft-ai/fork-axios-mock-adapter.git#built

GerkinDev added a commit to craft-ai/fork-axios-mock-adapter that referenced this issue Oct 30, 2024
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

Successfully merging a pull request may close this issue.

2 participants