Skip to content

Commit

Permalink
fix behaviour on failed postgres connection (#7287)
Browse files Browse the repository at this point in the history
Co-authored-by: joehan <joehanley@google.com>
  • Loading branch information
hlshen and joehan authored Jun 6, 2024
1 parent ccc2449 commit eb52df2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT

- Fix behaviour on failed postgres connection

## 0.2.5

- Icon fix
Expand Down
13 changes: 10 additions & 3 deletions firebase-vscode/src/data-connect/emulator-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from "node-fetch";
import { Observable, of } from "rxjs";
import { backOff } from "exponential-backoff";
import { ResolvedDataConnectConfigs } from "./config";
import { Signal } from "@preact/signals-core";

enum Kind {
KIND_UNSPECIFIED = "KIND_UNSPECIFIED",
Expand All @@ -28,19 +29,20 @@ export const emulatorOutputChannel =
vscode.window.createOutputChannel("Firebase Emulators");

/**
*
* TODO: convert to class
* @param fdcEndpoint FDC Emulator endpoint
*/
export async function runEmulatorIssuesStream(
configs: ResolvedDataConnectConfigs,
fdcEndpoint: string,
isPostgresEnabled: Signal<boolean>,
) {
const obsErrors = await getEmulatorIssuesStream(configs, fdcEndpoint);
const obsConverter = {
next(nextResponse: EmulatorIssueResponse) {
if (nextResponse.result?.issues?.length) {
for (const issue of nextResponse.result.issues) {
displayIssue(issue);
displayAndHandleIssue(issue, isPostgresEnabled);
}
}
},
Expand All @@ -57,14 +59,19 @@ export async function runEmulatorIssuesStream(
/**
* Based on the severity of the issue, either log, display notification, or display interactive popup to the user
*/
export function displayIssue(issue: EmulatorIssue) {
export function displayAndHandleIssue(issue: EmulatorIssue, isPostgresEnabled: Signal<boolean>) {
const issueMessage = `Data Connect Emulator: ${issue.kind.toString()} - ${issue.message}`;
if (issue.severity === Severity.ALERT) {
vscode.window.showErrorMessage(issueMessage);
} else if (issue.severity === Severity.NOTICE) {
vscode.window.showWarningMessage(issueMessage);
}
emulatorOutputChannel.appendLine(issueMessage);

// special handlings
if (issue.kind === Kind.SQL_CONNECTION) {
isPostgresEnabled.value = false;
}
}

/**
Expand Down
10 changes: 7 additions & 3 deletions firebase-vscode/src/data-connect/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class DataConnectEmulatorController implements vscode.Disposable {

// Notify webviews when the emulator status changes
effect(() => {
if (this.isPostgresEnabled) {
this.emulatorsController.emulatorStatusItem.show();
} else {
this.emulatorsController.emulatorStatusItem.hide();
}
notifyIsConnectedToPostgres(this.isPostgresEnabled.value);
}),

Expand Down Expand Up @@ -84,10 +89,9 @@ export class DataConnectEmulatorController implements vscode.Disposable {

// configure the emulator to use the local psql string
const emulatorClient = new DataConnectEmulatorClient();
emulatorClient.configureEmulator({ connectionString: newConnectionString });

this.isPostgresEnabled.value = true;
this.emulatorsController.emulatorStatusItem.show();

emulatorClient.configureEmulator({ connectionString: newConnectionString });
}

dispose() {
Expand Down
6 changes: 5 additions & 1 deletion firebase-vscode/src/data-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ export function registerFdc(
vscode.commands.executeCommand(
"firebase.dataConnect.executeIntrospection",
);
runEmulatorIssuesStream(configs, emulatorController.getLocalEndpoint().value);
runEmulatorIssuesStream(
configs,
emulatorController.getLocalEndpoint().value,
fdcEmulatorsController.isPostgresEnabled,
);
runDataConnectCompiler(configs, emulatorController.getLocalEndpoint().value);
}
}),
Expand Down

0 comments on commit eb52df2

Please sign in to comment.