import { BasicRepresentation } from '../../../../src/ldp/representation/BasicRepresentation'; import { ErrorToTemplateConverter } from '../../../../src/storage/conversion/ErrorToTemplateConverter'; import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError'; import { InternalServerError } from '../../../../src/util/errors/InternalServerError'; import { readableToString } from '../../../../src/util/StreamUtil'; import type { TemplateEngine } from '../../../../src/util/templates/TemplateEngine'; describe('An ErrorToTemplateConverter', (): void => { const identifier = { path: 'http://test.com/error' }; const templatePath = '/templates/codes'; const errorCode = 'E0001'; let templateEngine: jest.Mocked; let converter: ErrorToTemplateConverter; const preferences = {}; beforeEach(async(): Promise => { templateEngine = { render: jest.fn().mockReturnValue(Promise.resolve('')), }; converter = new ErrorToTemplateConverter(templateEngine, templatePath, '.html', 'text/html'); }); it('supports going from errors to the given content type.', async(): Promise => { await expect(converter.getInputTypes()).resolves.toEqual({ 'internal/error': 1 }); await expect(converter.getOutputTypes()).resolves.toEqual({ 'text/html': 1 }); }); it('does not support multiple errors.', async(): Promise => { const representation = new BasicRepresentation([ new Error('a'), new Error('b') ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); await expect(prom).rejects.toThrow('Only single errors are supported.'); await expect(prom).rejects.toThrow(InternalServerError); }); it('works with non-HTTP errors.', async(): Promise => { const error = new Error('error text'); const representation = new BasicRepresentation([ error ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); await expect(prom).resolves.toBeDefined(); const result = await prom; expect(result.binary).toBe(true); expect(result.metadata.contentType).toBe('text/html'); await expect(readableToString(result.data)).resolves.toBe(''); expect(templateEngine.render).toHaveBeenCalledTimes(1); expect(templateEngine.render).toHaveBeenLastCalledWith( { name: 'Error', message: 'error text', stack: error.stack }, ); }); it('calls the template engine with all HTTP error fields.', async(): Promise => { const error = new BadRequestHttpError('error text'); const representation = new BasicRepresentation([ error ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); templateEngine.render.mockRejectedValueOnce(new Error('error-specific template not found')); await expect(prom).resolves.toBeDefined(); const result = await prom; expect(result.binary).toBe(true); expect(result.metadata.contentType).toBe('text/html'); await expect(readableToString(result.data)).resolves.toBe(''); expect(templateEngine.render).toHaveBeenCalledTimes(2); expect(templateEngine.render).toHaveBeenNthCalledWith(1, {}, { templatePath: '/templates/codes', templateFile: 'H400.html' }); expect(templateEngine.render).toHaveBeenNthCalledWith(2, { name: 'BadRequestHttpError', message: 'error text', stack: error.stack }); }); it('only adds stack if it is defined.', async(): Promise => { const error = new BadRequestHttpError('error text'); delete error.stack; const representation = new BasicRepresentation([ error ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); templateEngine.render.mockRejectedValueOnce(new Error('error-specific template not found')); await expect(prom).resolves.toBeDefined(); const result = await prom; expect(result.binary).toBe(true); expect(result.metadata.contentType).toBe('text/html'); await expect(readableToString(result.data)).resolves.toBe(''); expect(templateEngine.render).toHaveBeenCalledTimes(2); expect(templateEngine.render).toHaveBeenNthCalledWith(1, {}, { templatePath: '/templates/codes', templateFile: 'H400.html' }); expect(templateEngine.render).toHaveBeenNthCalledWith(2, { name: 'BadRequestHttpError', message: 'error text' }); }); it('adds additional information if an error code description is found.', async(): Promise => { const error = new BadRequestHttpError('error text', { errorCode, details: { key: 'val' }}); const representation = new BasicRepresentation([ error ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); await expect(prom).resolves.toBeDefined(); const result = await prom; expect(result.binary).toBe(true); expect(result.metadata.contentType).toBe('text/html'); await expect(readableToString(result.data)).resolves.toBe(''); expect(templateEngine.render).toHaveBeenCalledTimes(2); expect(templateEngine.render).toHaveBeenNthCalledWith(1, { key: 'val' }, { templatePath: '/templates/codes', templateFile: 'E0001.html' }); expect(templateEngine.render).toHaveBeenNthCalledWith(2, { name: 'BadRequestHttpError', message: 'error text', stack: error.stack, description: '' }); }); it('sends an empty object for additional error code parameters if none are defined.', async(): Promise => { const error = new BadRequestHttpError('error text', { errorCode }); const representation = new BasicRepresentation([ error ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); await expect(prom).resolves.toBeDefined(); const result = await prom; expect(result.binary).toBe(true); expect(result.metadata.contentType).toBe('text/html'); await expect(readableToString(result.data)).resolves.toBe(''); expect(templateEngine.render).toHaveBeenCalledTimes(2); expect(templateEngine.render).toHaveBeenNthCalledWith(1, {}, { templatePath: '/templates/codes', templateFile: 'E0001.html' }); expect(templateEngine.render).toHaveBeenNthCalledWith(2, { name: 'BadRequestHttpError', message: 'error text', stack: error.stack, description: '' }); }); it('converts errors with a code as usual if no corresponding template is found.', async(): Promise => { const error = new BadRequestHttpError('error text', { errorCode: 'invalid' }); const representation = new BasicRepresentation([ error ], 'internal/error', false); const prom = converter.handle({ identifier, representation, preferences }); templateEngine.render.mockRejectedValueOnce(new Error('error-specific template not found')); await expect(prom).resolves.toBeDefined(); const result = await prom; expect(result.binary).toBe(true); expect(result.metadata.contentType).toBe('text/html'); await expect(readableToString(result.data)).resolves.toBe(''); expect(templateEngine.render).toHaveBeenCalledTimes(2); expect(templateEngine.render).toHaveBeenNthCalledWith(1, {}, { templatePath: '/templates/codes', templateFile: 'invalid.html' }); expect(templateEngine.render).toHaveBeenNthCalledWith(2, { name: 'BadRequestHttpError', message: 'error text', stack: error.stack }); }); });