Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log forwarding errors API #87

Merged
merged 4 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ Log Forwarding management API
* [.getSupportedDestinations()](#LogForwarding+getSupportedDestinations) ⇒ <code>Array.&lt;object&gt;</code>
* [.getDestinationSettings(destination)](#LogForwarding+getDestinationSettings) ⇒ <code>Array.&lt;object&gt;</code>
* [.setDestination(destination, config)](#LogForwarding+setDestination) ⇒ <code>Promise.&lt;\*&gt;</code>
* [.getErrors()](#LogForwarding+getErrors) ⇒ <code>object</code>

<a name="LogForwarding+get"></a>

Expand Down Expand Up @@ -447,6 +448,14 @@ Configure destination
| destination | <code>string</code> | Destination name |
| config | <code>object</code> | value-pairs of settings, specific to the destination |

<a name="LogForwarding+getErrors"></a>

### logForwarding.getErrors() ⇒ <code>object</code>
Get log forwarding errors

**Kind**: instance method of [<code>LogForwarding</code>](#LogForwarding)
**Returns**: <code>object</code> - Errors in format { destination: '<destination>', errors: [] }

<a name="LogForwardingLocalDestinationsProvider"></a>

## LogForwardingLocalDestinationsProvider
Expand Down
26 changes: 24 additions & 2 deletions src/LogForwarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ class LogForwarding {
}
}

/**
* Get log forwarding errors
*
* @returns {object} Errors in format { destination: '<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)
Expand All @@ -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),
Expand Down
59 changes: 57 additions & 2 deletions test/LogForwarding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ declare class LogForwarding {
* @returns response from set API
*/
setDestination(destination: string, config: any): Promise<any>;
/**
* Get log forwarding errors
* @returns Errors in format { destination: '<destination>', errors: [] }
*/
getErrors(): any;
}

/**
Expand Down