|
| 1 | +import fs from 'fs'; |
| 2 | + |
| 3 | +import { |
| 4 | + getErrorMessage, |
| 5 | + isErrorWithCode, |
| 6 | + isErrorWithMessage, |
| 7 | + isErrorWithStack, |
| 8 | + wrapError, |
| 9 | +} from './errors'; |
| 10 | + |
| 11 | +describe('isErrorWithCode', () => { |
| 12 | + it('returns true if given an object that includes a "code" property', () => { |
| 13 | + expect( |
| 14 | + isErrorWithCode({ code: 'some code', message: 'some message' }), |
| 15 | + ).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns false if given null', () => { |
| 19 | + expect(isErrorWithCode(null)).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns false if given undefined', () => { |
| 23 | + expect(isErrorWithCode(undefined)).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns false if given something that is not typeof object', () => { |
| 27 | + expect(isErrorWithCode(12345)).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns false if given an empty object', () => { |
| 31 | + expect(isErrorWithCode({})).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns false if given a non-empty object that does not have a "code" property', () => { |
| 35 | + expect(isErrorWithCode({ message: 'some message' })).toBe(false); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('isErrorWithMessage', () => { |
| 40 | + it('returns true if given an object that includes a "message" property', () => { |
| 41 | + expect( |
| 42 | + isErrorWithMessage({ code: 'some code', message: 'some message' }), |
| 43 | + ).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns false if given null', () => { |
| 47 | + expect(isErrorWithMessage(null)).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns false if given undefined', () => { |
| 51 | + expect(isErrorWithMessage(undefined)).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns false if given something that is not typeof object', () => { |
| 55 | + expect(isErrorWithMessage(12345)).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns false if given an empty object', () => { |
| 59 | + expect(isErrorWithMessage({})).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns false if given a non-empty object that does not have a "message" property', () => { |
| 63 | + expect(isErrorWithMessage({ code: 'some code' })).toBe(false); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('isErrorWithStack', () => { |
| 68 | + it('returns true if given an object that includes a "stack" property', () => { |
| 69 | + expect(isErrorWithStack({ code: 'some code', stack: 'some stack' })).toBe( |
| 70 | + true, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns false if given null', () => { |
| 75 | + expect(isErrorWithStack(null)).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns false if given undefined', () => { |
| 79 | + expect(isErrorWithStack(undefined)).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns false if given something that is not typeof object', () => { |
| 83 | + expect(isErrorWithStack(12345)).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns false if given an empty object', () => { |
| 87 | + expect(isErrorWithStack({})).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns false if given a non-empty object that does not have a "stack" property', () => { |
| 91 | + expect( |
| 92 | + isErrorWithStack({ code: 'some code', message: 'some message' }), |
| 93 | + ).toBe(false); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('wrapError', () => { |
| 98 | + describe('if the original error is an Error instance not generated by fs.promises', () => { |
| 99 | + it('returns a new Error with the given message', () => { |
| 100 | + const originalError = new Error('oops'); |
| 101 | + |
| 102 | + const newError = wrapError(originalError, 'Some message'); |
| 103 | + |
| 104 | + expect(newError.message).toBe('Some message'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('links to the original error via "cause"', () => { |
| 108 | + const originalError = new Error('oops'); |
| 109 | + |
| 110 | + const newError = wrapError(originalError, 'Some message'); |
| 111 | + |
| 112 | + expect(newError.cause).toBe(originalError); |
| 113 | + }); |
| 114 | + |
| 115 | + it('copies over any "code" property that exists on the given Error', () => { |
| 116 | + const originalError = new Error('oops'); |
| 117 | + // @ts-expect-error The Error interface doesn't have a "code" property |
| 118 | + originalError.code = 'CODE'; |
| 119 | + |
| 120 | + const newError = wrapError(originalError, 'Some message'); |
| 121 | + |
| 122 | + expect(newError.code).toBe('CODE'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('if the original error was generated by fs.promises', () => { |
| 127 | + it('returns a new Error with the given message', async () => { |
| 128 | + let originalError; |
| 129 | + try { |
| 130 | + await fs.promises.readFile('/tmp/nonexistent', 'utf8'); |
| 131 | + } catch (error: any) { |
| 132 | + originalError = error; |
| 133 | + } |
| 134 | + |
| 135 | + const newError = wrapError(originalError, 'Some message'); |
| 136 | + |
| 137 | + expect(newError.message).toBe('Some message'); |
| 138 | + }); |
| 139 | + |
| 140 | + it("links to the original error via 'cause'", async () => { |
| 141 | + let originalError; |
| 142 | + try { |
| 143 | + await fs.promises.readFile('/tmp/nonexistent', 'utf8'); |
| 144 | + } catch (error: any) { |
| 145 | + originalError = error; |
| 146 | + } |
| 147 | + |
| 148 | + const newError = wrapError(originalError, 'Some message'); |
| 149 | + |
| 150 | + expect(newError.cause).toBe(originalError); |
| 151 | + }); |
| 152 | + |
| 153 | + it('copies over any "code" property that exists on the given Error', async () => { |
| 154 | + let originalError; |
| 155 | + try { |
| 156 | + await fs.promises.readFile('/tmp/nonexistent', 'utf8'); |
| 157 | + } catch (error: any) { |
| 158 | + originalError = error; |
| 159 | + } |
| 160 | + |
| 161 | + const newError = wrapError(originalError, 'Some message'); |
| 162 | + |
| 163 | + expect(newError.code).toBe('ENOENT'); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('if the original error is an object but not an Error instance', () => { |
| 168 | + describe('if the message is a non-empty string', () => { |
| 169 | + it('combines a string version of the original error and message together in a new Error', () => { |
| 170 | + const originalError = { some: 'error' }; |
| 171 | + |
| 172 | + const newError = wrapError(originalError, 'Some message'); |
| 173 | + |
| 174 | + expect(newError.message).toBe('[object Object]: Some message'); |
| 175 | + }); |
| 176 | + |
| 177 | + it('does not set a cause on the new Error', async () => { |
| 178 | + const originalError = { some: 'error' }; |
| 179 | + |
| 180 | + const newError = wrapError(originalError, 'Some message'); |
| 181 | + |
| 182 | + expect(newError.cause).toBeUndefined(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('does not set a code on the new Error', async () => { |
| 186 | + const originalError = { some: 'error' }; |
| 187 | + |
| 188 | + const newError = wrapError(originalError, 'Some message'); |
| 189 | + |
| 190 | + expect(newError.code).toBeUndefined(); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + describe('if the message is an empty string', () => { |
| 195 | + it('places a string version of the original error in a new Error object without an additional message', () => { |
| 196 | + const originalError = { some: 'error' }; |
| 197 | + |
| 198 | + const newError = wrapError(originalError, ''); |
| 199 | + |
| 200 | + expect(newError.message).toBe('[object Object]'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('does not set a cause on the new Error', async () => { |
| 204 | + const originalError = { some: 'error' }; |
| 205 | + |
| 206 | + const newError = wrapError(originalError, ''); |
| 207 | + |
| 208 | + expect(newError.cause).toBeUndefined(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('does not set a code on the new Error', async () => { |
| 212 | + const originalError = { some: 'error' }; |
| 213 | + |
| 214 | + const newError = wrapError(originalError, ''); |
| 215 | + |
| 216 | + expect(newError.code).toBeUndefined(); |
| 217 | + }); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + describe('if the original error is a string', () => { |
| 222 | + describe('if the message is a non-empty string', () => { |
| 223 | + it('combines the original error and message together in a new Error', () => { |
| 224 | + const newError = wrapError('Some original message', 'Some message'); |
| 225 | + |
| 226 | + expect(newError.message).toBe('Some original message: Some message'); |
| 227 | + }); |
| 228 | + |
| 229 | + it('does not set a cause on the new Error', () => { |
| 230 | + const newError = wrapError('Some original message', 'Some message'); |
| 231 | + |
| 232 | + expect(newError.cause).toBeUndefined(); |
| 233 | + }); |
| 234 | + |
| 235 | + it('does not set a code on the new Error', () => { |
| 236 | + const newError = wrapError('Some original message', 'Some message'); |
| 237 | + |
| 238 | + expect(newError.code).toBeUndefined(); |
| 239 | + }); |
| 240 | + }); |
| 241 | + |
| 242 | + describe('if the message is an empty string', () => { |
| 243 | + it('places the original error in a new Error object without an additional message', () => { |
| 244 | + const newError = wrapError('Some original message', ''); |
| 245 | + |
| 246 | + expect(newError.message).toBe('Some original message'); |
| 247 | + }); |
| 248 | + |
| 249 | + it('does not set a cause on the new Error', () => { |
| 250 | + const newError = wrapError('Some original message', ''); |
| 251 | + |
| 252 | + expect(newError.cause).toBeUndefined(); |
| 253 | + }); |
| 254 | + |
| 255 | + it('does not set a code on the new Error', () => { |
| 256 | + const newError = wrapError('Some original message', ''); |
| 257 | + |
| 258 | + expect(newError.code).toBeUndefined(); |
| 259 | + }); |
| 260 | + }); |
| 261 | + }); |
| 262 | +}); |
| 263 | + |
| 264 | +describe('getErrorMessage', () => { |
| 265 | + it("returns the value of the 'message' property from the given object if it is present", () => { |
| 266 | + expect(getErrorMessage({ message: 'hello' })).toBe('hello'); |
| 267 | + }); |
| 268 | + |
| 269 | + it("returns the result of calling .toString() on the given object if it has no 'message' property", () => { |
| 270 | + expect(getErrorMessage({ foo: 'bar' })).toBe('[object Object]'); |
| 271 | + }); |
| 272 | + |
| 273 | + it('returns the result of calling .toString() on the given non-object', () => { |
| 274 | + expect(getErrorMessage(42)).toBe('42'); |
| 275 | + }); |
| 276 | + |
| 277 | + it('returns an empty string if given null', () => { |
| 278 | + expect(getErrorMessage(null)).toBe(''); |
| 279 | + }); |
| 280 | + |
| 281 | + it('returns an empty string if given undefined', () => { |
| 282 | + expect(getErrorMessage(undefined)).toBe(''); |
| 283 | + }); |
| 284 | +}); |
0 commit comments