diff --git a/src/soap-providers.spec.ts b/src/soap-providers.spec.ts index 9d61900..6f83aed 100644 --- a/src/soap-providers.spec.ts +++ b/src/soap-providers.spec.ts @@ -1,4 +1,8 @@ -import { SoapModuleOptions, SoapModuleAsyncOptions, SoapModuleOptionsFactory } from './soap-module-options.type'; +import { + SoapModuleOptions, + SoapModuleAsyncOptions, + SoapModuleOptionsFactory, +} from './soap-module-options.type'; import { buildAsyncProviders, buildClientProvider } from './soap-providers'; import { FactoryProvider } from '@nestjs/common'; @@ -21,7 +25,7 @@ describe('SoapProviders', () => { } as SoapModuleOptions; const optionsFactory = { - createSoapModuleOptions: () => soapOptions + createSoapModuleOptions: () => soapOptions, } as SoapModuleOptionsFactory; soapOptionsAsync = [ @@ -104,5 +108,11 @@ describe('SoapProviders', () => { expect(result).toEqual(expectedResult); }); + + it('Should throw error when no option is provided', () => { + const options = { clientName: 'Test' }; + + return expect(() => buildAsyncProviders(options)).toThrowError(); + }); }); }); diff --git a/src/soap-providers.ts b/src/soap-providers.ts index f76966e..a8be409 100644 --- a/src/soap-providers.ts +++ b/src/soap-providers.ts @@ -5,9 +5,7 @@ import { SoapService } from './soap.service'; export const buildClientProvider = (clientName: string): FactoryProvider => ({ provide: clientName, - useFactory: async (soapService: SoapService) => { - return await soapService.createAsyncClient(); - }, + useFactory: async (soapService: SoapService) => await soapService.createAsyncClient(), inject: [SoapService], }); @@ -18,7 +16,9 @@ export const buildAsyncProviders = (soapAsyncOptions: SoapModuleAsyncOptions): P if (useExisting) return createUseExistingProvider(soapAsyncOptions); if (useFactory) return createUseFactoryProvider(soapAsyncOptions); - throw new Error('[SoapModule]: useClass, useExisting or useFactory must be filled when using async options.'); + throw new Error( + '[SoapModule]: useClass, useExisting or useFactory must be filled when using async options.', + ); }; const createUseClassProvider = (option: SoapModuleAsyncOptions): Provider[] => { diff --git a/src/soap.service.spec.ts b/src/soap.service.spec.ts index 8753ce5..4266dd9 100644 --- a/src/soap.service.spec.ts +++ b/src/soap.service.spec.ts @@ -5,7 +5,6 @@ import { Client, createClientAsync } from 'soap'; import { MaybeMocked } from 'ts-jest/dist/utils/testing'; import { SoapModuleOptions } from 'src'; import { SOAP_MODULE_OPTIONS } from './soap-constants'; -import { ServiceUnavailableException } from '@nestjs/common'; const soapModuleOptionsMock = { uri: 'some-uri', @@ -46,10 +45,10 @@ describe('SoapService', () => { clientMock.setSecurity = jest.fn(); }); - it('Should throw service unavailable on connection issue', () => { + it('Should return null on connection issue', () => { soapCreateClientAsyncMock.mockRejectedValue('some error'); - return expect(service.createAsyncClient()).rejects.toThrowError(ServiceUnavailableException); + return expect(service.createAsyncClient()).resolves.toBeNull(); }); it('Should create client with soap options data', async () => { diff --git a/src/soap.service.ts b/src/soap.service.ts index 6096ff1..a24cc6e 100644 --- a/src/soap.service.ts +++ b/src/soap.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, ServiceUnavailableException } from '@nestjs/common'; +import { Inject, Injectable, Logger } from '@nestjs/common'; import { SoapModuleOptions } from './soap-module-options.type'; import { SOAP_MODULE_OPTIONS } from './soap-constants'; import { BasicAuthSecurity, Client, createClientAsync, ISecurity } from 'soap'; @@ -10,19 +10,28 @@ export class SoapService { async createAsyncClient(): Promise { const options = this.soapModuleOptions; - const client = await createClientAsync(options.uri, options.clientOptions)?.catch((err) => { - throw new ServiceUnavailableException(err); - }); + try { + const client = await createClientAsync(options.uri, options.clientOptions); - if (!options.auth) return client; + if (!options.auth) return client; - const basicAuth: ISecurity = new BasicAuthSecurity( - options.auth.username, - options.auth.password, - ); + const basicAuth: ISecurity = new BasicAuthSecurity( + options.auth.username, + options.auth.password, + ); - client.setSecurity(basicAuth); + client.setSecurity(basicAuth); - return client; + return client; + + } catch (err) { + const logger = new Logger('SoapModule'); + + logger.error( + `${err.message} \n - An error occurred while creating the soap client. Check the SOAP service URL and status.`, + ); + + return null; + } } }