diff --git a/README.md b/README.md index 38654fa..23af882 100644 --- a/README.md +++ b/README.md @@ -365,6 +365,7 @@ Log Forwarding management API * [.getSupportedDestinations()](#LogForwarding+getSupportedDestinations) ⇒ Array.<object> * [.getDestinationSettings(destination)](#LogForwarding+getDestinationSettings) ⇒ Array.<object> * [.setDestination(destination, config)](#LogForwarding+setDestination) ⇒ Promise.<\*> + * [.getErrors()](#LogForwarding+getErrors) ⇒ object @@ -447,6 +448,14 @@ Configure destination | destination | string | Destination name | | config | object | value-pairs of settings, specific to the destination | + + +### logForwarding.getErrors() ⇒ object +Get log forwarding errors + +**Kind**: instance method of [LogForwarding](#LogForwarding) +**Returns**: object - Errors in format { destination: '', errors: [] } + ## LogForwardingLocalDestinationsProvider diff --git a/src/LogForwarding.js b/src/LogForwarding.js index 8bbae07..f818805 100644 --- a/src/LogForwarding.js +++ b/src/LogForwarding.js @@ -126,6 +126,28 @@ class LogForwarding { } } + /** + * Get log forwarding errors + * + * @returns {object} Errors in format { destination: '', errors: [] } + */ + async getErrors () { + try { + const requestResult = await this.request('get', undefined, '/errors') + const result = await requestResult.json() + if (result.destination !== undefined && result.errors !== undefined && Array.isArray(result.errors)) { + return result + } else { + return { + destination: undefined, + errors: [] + } + } + } catch (e) { + throw new Error(`Could not get log forwarding errors for namespace '${this.namespace}': ${e.message}`) + } + } + async set (data) { try { const res = await this.request('put', data) @@ -135,14 +157,14 @@ class LogForwarding { } } - async request (method, data) { + async request (method, data, subPath = '') { if (this.namespace === '_') { throw new Error("Namespace '_' is not supported by log forwarding management API") } const fetch = createFetch() const res = await fetch( - this.apiHost + '/runtime/namespaces/' + this.namespace + '/logForwarding', + this.apiHost + '/runtime/namespaces/' + this.namespace + '/logForwarding' + subPath, { method: method, body: JSON.stringify(data), diff --git a/test/LogForwarding.test.js b/test/LogForwarding.test.js index 26c25fa..d59237c 100644 --- a/test/LogForwarding.test.js +++ b/test/LogForwarding.test.js @@ -174,8 +174,63 @@ test('set destination failed', async () => { .rejects.toThrow("Could not update log forwarding settings for namespace 'some_namespace': mocked error") }) -const assertRequest = (expectedMethod, expectedData) => { - expect(mockFetch).toBeCalledWith(apiUrl, { +test.each([ + [ + 'errors exist', + { + destination: 'destination', + errors: [ + 'error1', + 'error2' + ] + }, + { + destination: 'destination', + errors: [ + 'error1', + 'error2' + ] + } + ], + [ + 'no errors', + { + destination: 'destination', + errors: [] + }, + { + destination: 'destination', + errors: [] + } + ], + [ + 'empty remote response', + {}, + { + destination: undefined, + errors: [] + } + ] +])('get errors (%s)', async (test, remoteResponse, expected) => { + mockFetch.mockReturnValue(new Promise(resolve => { + resolve({ + ok: true, + json: jest.fn().mockResolvedValue(remoteResponse) + }) + })) + expect(await logForwarding.getErrors()).toEqual(expected) + expect(mockFetch).toBeCalledTimes(1) + assertRequest('get', undefined, '/errors') +}) + +test('could not get errors', async () => { + mockFetch.mockRejectedValue(new Error('mocked error')) + await expect(logForwarding.getErrors()) + .rejects.toThrow("Could not get log forwarding errors for namespace 'some_namespace': mocked error") +}) + +const assertRequest = (expectedMethod, expectedData, expectedSubPath = '') => { + expect(mockFetch).toBeCalledWith(apiUrl + expectedSubPath, { method: expectedMethod, body: JSON.stringify(expectedData), headers: { diff --git a/types.d.ts b/types.d.ts index 19bce75..35f60d4 100644 --- a/types.d.ts +++ b/types.d.ts @@ -47,6 +47,11 @@ declare class LogForwarding { * @returns response from set API */ setDestination(destination: string, config: any): Promise; + /** + * Get log forwarding errors + * @returns Errors in format { destination: '', errors: [] } + */ + getErrors(): any; } /**