Skip to content

Commit

Permalink
Fix database emulator rules error parsing. Fix #4454.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchenshi committed Apr 27, 2022
1 parent 58a95e0 commit c26d990
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Updates `superstatic` to `v8` to fix audit issues.
- Make grantToken tenant-aware (#4475)
- Fix database emulator rules error parsing (#4454).
45 changes: 40 additions & 5 deletions src/emulator/databaseEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ export class DatabaseEmulator implements EmulatorInstance {
if (!c.instance) {
continue;
}

await this.updateRules(c.instance, c.rules);
try {
await this.updateRules(c.instance, c.rules);
} catch (e: any) {
const rulesError = this.prettyPrintRulesError(c.rules, e);
this.logger.logLabeled("WARN", "database", rulesError);
this.logger.logLabeled("WARN", "database", "Failed to update rules");
throw new FirebaseError(
`Failed to load initial ${Constants.description(this.getName())} rules:\n${rulesError}`
);
}
}
}
}
Expand Down Expand Up @@ -173,12 +181,39 @@ export class DatabaseEmulator implements EmulatorInstance {
if (e.context && e.context.body) {
throw e.context.body.error;
}
throw e.original;
if (e.original) {
throw e.original;
}
throw e;
}
}

private prettyPrintRulesError(filePath: string, error: string): string {
private prettyPrintRulesError(filePath: string, error: unknown): string {
let errStr;
switch (typeof error) {
case "string":
errStr = error;
break;
case "object":
if (error != null && "message" in error) {
const message = (error as { message: unknown }).message;
errStr = `${message}`;
if (typeof message === "string") {
try {
// message may be JSON with {error: string} in it
const parsed = JSON.parse(message);
if (typeof parsed == "object" && parsed.error) {
errStr = `${parsed.error}`;
}
} catch (_) {}
}
break;
}
// fallthrough
default:
errStr = `Unknown error: ${JSON.stringify(error)}`;
}
const relativePath = path.relative(process.cwd(), filePath);
return `${clc.cyan(relativePath)}:${error.trim()}`;
return `${clc.cyan(relativePath)}:${errStr.trim()}`;
}
}

0 comments on commit c26d990

Please sign in to comment.