Skip to content

Commit

Permalink
#16 - Improvement on createClient error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lehh committed Feb 11, 2022
1 parent 2cc9702 commit 8f6017b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
14 changes: 12 additions & 2 deletions src/soap-providers.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -21,7 +25,7 @@ describe('SoapProviders', () => {
} as SoapModuleOptions;

const optionsFactory = {
createSoapModuleOptions: () => soapOptions
createSoapModuleOptions: () => soapOptions,
} as SoapModuleOptionsFactory;

soapOptionsAsync = [
Expand Down Expand Up @@ -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();
});
});
});
8 changes: 4 additions & 4 deletions src/soap-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});

Expand All @@ -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[] => {
Expand Down
5 changes: 2 additions & 3 deletions src/soap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 () => {
Expand Down
31 changes: 20 additions & 11 deletions src/soap.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,19 +10,28 @@ export class SoapService {
async createAsyncClient(): Promise<Client> {
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;
}
}
}

0 comments on commit 8f6017b

Please sign in to comment.