Skip to content

Commit

Permalink
frontend: util: Add combineClusterListErrors
Browse files Browse the repository at this point in the history
For errors, we may need to combine several cluster->errors
objects into a single object.

This is in preparation for multi cluster.

Signed-off-by: René Dudfield <renedudfield@microsoft.com>
Co-authored-by: Joaquim Rocha <joaquim.rocha@microsoft.com>
Signed-off-by: Joaquim Rocha <joaquim.rocha@microsoft.com>
  • Loading branch information
illume and joaquimrocha committed Oct 15, 2024
1 parent c10f113 commit d1ac34e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
41 changes: 40 additions & 1 deletion frontend/src/lib/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flattenClusterListItems } from './util';
import { combineClusterListErrors, flattenClusterListItems } from './util';

describe('flattenClusterListItems', () => {
it('should return a flattened list of items', () => {
Expand Down Expand Up @@ -29,3 +29,42 @@ describe('flattenClusterListItems', () => {
expect(result).toEqual([1, 2, 3, 4]);
});
});

describe('combineClusterListErrors', () => {
it('should return null if there are no errors', () => {
const result = combineClusterListErrors(null, null);
expect(result).toBeNull();
});

it('should combine errors from multiple clusters', () => {
const error1 = { message: 'Error 1', status: 500, name: 'InternalServerError' };
const error2 = { message: 'Error 2', status: 404, name: 'NotFoundError' };
const clusterErrors1 = { clusterA: error1 };
const clusterErrors2 = { clusterB: error2 };

const result = combineClusterListErrors(clusterErrors1, clusterErrors2);
expect(result).toEqual({
clusterA: error1,
clusterB: error2,
});
});

it('should ignore null errors', () => {
const error1 = { message: 'Error 1', status: 500, name: 'InternalServerError' };
const clusterErrors1 = { clusterA: error1 };
const clusterErrors2 = { clusterB: null };

const result = combineClusterListErrors(clusterErrors1, clusterErrors2);
expect(result).toEqual({
clusterA: error1,
});
});

it('should return null if all errors are null', () => {
const clusterErrors1 = { clusterA: null };
const clusterErrors2 = { clusterB: null };

const result = combineClusterListErrors(clusterErrors1, clusterErrors2);
expect(result).toBeNull();
});
});
23 changes: 23 additions & 0 deletions frontend/src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import humanizeDuration from 'humanize-duration';
import merge from 'lodash/merge';
import React from 'react';
import { useHistory } from 'react-router';
import { filterGeneric, filterResource } from '../redux/filterSlice';
Expand Down Expand Up @@ -210,6 +211,28 @@ export function flattenClusterListItems<T>(
return flatItems.length > 0 ? flatItems : null;
}

/**
* Combines errors per cluster.
*
* @param args The list of errors per cluster to join.
* @returns The joint list of errors, or null if there are no errors.
*/
export function combineClusterListErrors(
...args: ({ [cluster: string]: ApiError | null } | null)[]
): { [cluster: string]: ApiError | null } | null {
const filteredArgs = args.map(clusterErrors => {
if (clusterErrors === null) {
return {};
}
return Object.fromEntries(Object.entries(clusterErrors).filter(([, error]) => error !== null));
});

const errors = merge({}, ...filteredArgs);
const hasErrors = Object.values(errors).some(error => error !== null);

return hasErrors ? errors : null;
}

type URLStateParams<T> = {
/** The defaultValue for the URL state. */
defaultValue: T;
Expand Down

0 comments on commit d1ac34e

Please sign in to comment.