-
Notifications
You must be signed in to change notification settings - Fork 65
feat(state-management): add error handling and service resolution to … #2518
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import { Publisher } from './Publisher'; | ||
import type { Webview } from '@podman-desktop/api'; | ||
import { | ||
ApplicationStateError, | ||
ApplicationStateErrorType, | ||
type ApplicationStateErrorDetails, | ||
} from '@shared/src/models/IError'; | ||
import type { Messages } from '@shared/Messages'; | ||
|
||
/** | ||
* Base class for managing state with persistence and error handling | ||
*/ | ||
export abstract class StateManager<T> extends Publisher<T> { | ||
constructor(webview: Webview, channel: Messages, stateGetter: () => T) { | ||
super(webview, channel, stateGetter); | ||
} | ||
|
||
/** | ||
* Persists the current state and notifies subscribers | ||
* @throws {ApplicationStateError} if persistence fails | ||
*/ | ||
protected async persistState(): Promise<void> { | ||
try { | ||
await this.notify(); | ||
} catch (err: unknown) { | ||
const details: ApplicationStateErrorDetails = { | ||
operation: 'persist', | ||
timestamp: Date.now(), | ||
}; | ||
throw new ApplicationStateError(ApplicationStateErrorType.PERSISTENCE_ERROR, 'Failed to persist state', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have the same definition for Here we just send the information to the frontend (using notify), so there is nothing persisted, if the user open and leave the webview, the resources are lost, they are very volatile. |
||
originalError: err, | ||
details, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Loads persisted state | ||
* @throws {ApplicationStateError} if loading fails | ||
*/ | ||
protected async loadPersistedState(): Promise<void> { | ||
try { | ||
await this.refresh(); | ||
} catch (err: unknown) { | ||
const details: ApplicationStateErrorDetails = { | ||
operation: 'load', | ||
timestamp: Date.now(), | ||
}; | ||
throw new ApplicationStateError(ApplicationStateErrorType.LOAD_ERROR, 'Failed to load persisted state', { | ||
originalError: err, | ||
details, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Refreshes the current state | ||
* Should be implemented by derived classes | ||
*/ | ||
protected abstract refresh(): Promise<void>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { describe, expect, it, vi, beforeEach, type Mock } from 'vitest'; | ||
import { Publisher } from '../Publisher'; | ||
import type { Webview } from '@podman-desktop/api'; | ||
import { Messages } from '@shared/Messages'; | ||
|
||
interface TestState { | ||
testData: string; | ||
} | ||
|
||
describe('Publisher', () => { | ||
let mockWebview: Webview; | ||
let publisher: Publisher<TestState>; | ||
let mockStateGetter: Mock<() => TestState>; | ||
|
||
beforeEach(() => { | ||
mockWebview = { | ||
postMessage: vi.fn().mockImplementation(() => Promise.resolve()), | ||
} as unknown as Webview; | ||
|
||
mockStateGetter = vi.fn().mockReturnValue({ testData: 'test' }); | ||
publisher = new Publisher<TestState>(mockWebview, Messages.MSG_ASK_ERROR_STATE, mockStateGetter); | ||
}); | ||
|
||
it('should successfully notify with state data', async () => { | ||
await publisher['notify'](); | ||
|
||
expect(mockStateGetter).toHaveBeenCalled(); | ||
expect(mockWebview.postMessage).toHaveBeenCalledWith({ | ||
id: Messages.MSG_ASK_ERROR_STATE, | ||
body: { testData: 'test' }, | ||
}); | ||
}); | ||
|
||
it('should handle postMessage rejection by re-throwing', async () => { | ||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
const error = new Error('Communication failed'); | ||
(mockWebview.postMessage as Mock).mockRejectedValueOnce(error); | ||
|
||
await expect(publisher['notify']()).rejects.toThrow(error); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Error publishing to'), error); | ||
|
||
consoleSpy.mockRestore(); | ||
}); | ||
|
||
it('should handle stateGetter errors by re-throwing', async () => { | ||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
const error = new Error('State getter failed'); | ||
mockStateGetter.mockImplementation(() => { | ||
throw error; | ||
}); | ||
|
||
await expect(publisher['notify']()).rejects.toThrow(error); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Error publishing to'), error); | ||
|
||
consoleSpy.mockRestore(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
export enum ApplicationStateErrorType { | ||
PERSISTENCE_ERROR = 'PERSISTENCE_ERROR', | ||
LOAD_ERROR = 'LOAD_ERROR', | ||
POD_ERROR = 'POD_ERROR', | ||
RESOURCE_ERROR = 'RESOURCE_ERROR', | ||
GPU_ERROR = 'GPU_ERROR', | ||
CLEANUP_ERROR = 'CLEANUP_ERROR', | ||
MODEL_ERROR = 'MODEL_ERROR', | ||
INFERENCE_ERROR = 'INFERENCE_ERROR', | ||
UNKNOWN = 'UNKNOWN', | ||
} | ||
|
||
export class ApplicationStateError extends Error { | ||
constructor( | ||
public type: ApplicationStateErrorType, | ||
message: string, | ||
public details?: unknown, | ||
) { | ||
super(message); | ||
this.name = 'ApplicationStateError'; | ||
} | ||
} | ||
|
||
export interface ApplicationStateErrorDetails { | ||
recipeId?: string; | ||
modelId?: string; | ||
podId?: string; | ||
operation?: string; | ||
timestamp?: number; | ||
resourceType?: 'memory' | 'cpu' | 'gpu' | 'disk' | 'model'; | ||
resourceDetails?: { | ||
required?: number; | ||
available?: number; | ||
unit?: string; | ||
}; | ||
cleanupStage?: 'extension' | 'playground' | 'recipe' | 'container' | 'image'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the time when we created the
Publisher
class, we made it so the notify was not a promise, as the caller often do not want to deal with a promise.The idea is also to consider that the notify cannot really fails, so there is no need to catch the
error(s)
. The rare cases where it could fails would be:IMHO those two edge case are not worth the need effort to add a try catch every-time we want to call the
notify
function.