Skip to content

Commit

Permalink
reactor(qb): throw error instead of returning it
Browse files Browse the repository at this point in the history
  • Loading branch information
donatandelic committed Feb 7, 2024
1 parent 707d779 commit 70b0ddb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
30 changes: 15 additions & 15 deletions src/querybuilder/statements/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { validate as validateJsonSchema } from "jsonschema";
import { StatementHistory } from "./historyProvider.js";
import { OrbisDB, OrbisDocument } from "../../index.js";
import { catchError } from "../../util/tryit.js";
import { OrbisError } from "../../util/results.js";

export class BulkInsertStatement<
T = Record<string, any>,
Expand Down Expand Up @@ -302,29 +303,28 @@ export class InsertStatement<T = Record<string, any>> extends StatementHistory {
model,
};

try {
const document = await this.#orbis.ceramic.createDocument(query);

super.storeResult({
timestamp,
success: true,
result: document,
query,
});
const [document, error] = await catchError(() =>
this.#orbis.ceramic.createDocument(query)
);

return document;
} catch (error) {
if (error) {
super.storeResult({
timestamp,
success: false,
error,
query,
});

return {
query,
error,
};
throw new OrbisError(error.message, { error, query });
}

super.storeResult({
timestamp,
success: true,
result: document,
query,
});

return document;
}
}
3 changes: 2 additions & 1 deletion src/querybuilder/statements/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StatementHistory } from "./historyProvider.js";
import { OrderByParams, SqlSelectBuilder } from "./sqlbuild/index.js";

import { catchError } from "../../util/tryit.js";
import { OrbisError } from "../../util/results.js";

// TODO: Improve typing for operators (and enable nested queries)
export class SelectStatement<T = Record<string, any>> extends StatementHistory {
Expand Down Expand Up @@ -211,7 +212,7 @@ export class SelectStatement<T = Record<string, any>> extends StatementHistory {
timestamp,
});

return { query, error };
throw new OrbisError(error.message, { error, query });
}

super.storeResult({
Expand Down
13 changes: 9 additions & 4 deletions src/querybuilder/statements/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { StreamID } from "@ceramicnetwork/streamid";
import { OrbisDB } from "../../index.js";
import { StatementHistory } from "./historyProvider.js";
import { catchError } from "../../util/tryit.js";
import { OrbisError } from "../../util/results.js";

export class UpdateByIdStatement<
T = Record<string, any>,
Expand Down Expand Up @@ -107,12 +108,16 @@ export class UpdateByIdStatement<
query,
});

return {
query,
error,
};
throw new OrbisError(error.message, { error, query });
}

super.storeResult({
timestamp,
success: true,
result: document,
query,
});

return document;
}
}
Expand Down

0 comments on commit 70b0ddb

Please sign in to comment.