Skip to content

Commit

Permalink
fix: better error handling for users controllers
Browse files Browse the repository at this point in the history
fix: use default data adaptor for sign-in and sign-up process
  • Loading branch information
jairmilanes committed Dec 11, 2022
1 parent 47ba2c2 commit f3f46a5
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 19 deletions.
2 changes: 0 additions & 2 deletions src/client/_public/bundles/dashboard.runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
console.info(process.cwd());

const defaults = require("./defaults.runtime");
const helpers = require("../../_helpers");

Expand Down
4 changes: 2 additions & 2 deletions src/client/_public/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ $.fn.modal = function (name, context) {

$.fn.confirm = function (name, context, callback) {
this.render(name, context, "append");
console.log(`#${context.id}`);

const modal = $(`#${context.id}`);
console.log(modal);

$("[data-modal-confirm]", modal).click(() => {
callback(modal);
});
Expand Down
2 changes: 0 additions & 2 deletions src/client/_public/hotbars/emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ function EmailPage() {
};

$("body").confirm("_confirm-modal", context, async (modal) => {
console.log("Confirmed", this);

await this.remove(id);
await this.load(this.page);

Expand Down
2 changes: 1 addition & 1 deletion src/server/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class AuthManager {
passport.deserializeUser(function (userId: any, cb) {
logger.debug("%P Auth:Deserialize session user %s", 2, userId);
process.nextTick(function () {
const usersDb = DataManager.get("jsonDb").from(
const usersDb = DataManager.get().from(
Config.get<string>("auth.usersTable")
);
return usersDb
Expand Down
6 changes: 3 additions & 3 deletions src/server/auth/strategies/json-db.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export abstract class JsonDbAuthStrategy extends LocalAuthStrategyAbstract {
}

async getUser(username: string): Promise<User> {
const usersDb = DataManager.get("jsonDb").from(
const usersDb = DataManager.get().from(
Config.get<string>("auth.usersTable")
);

Expand All @@ -25,7 +25,7 @@ export abstract class JsonDbAuthStrategy extends LocalAuthStrategyAbstract {
async createUser(data: Record<string, any>) {
const profile = _.pick(data, ["username", "email", "password"]) as NewUser;

const usersDb = DataManager.get("jsonDb").from(
const usersDb = DataManager.get().from(
Config.get<string>("auth.usersTable")
);

Expand All @@ -41,7 +41,7 @@ export abstract class JsonDbAuthStrategy extends LocalAuthStrategyAbstract {
}

async confirmEmail(username: string): Promise<User> {
const usersDb = DataManager.get("jsonDb").from(
const usersDb = DataManager.get().from(
Config.get<string>("auth.usersTable")
);

Expand Down
22 changes: 14 additions & 8 deletions src/server/controllers/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export class Controllers {
const controllers = glob.sync(Config.fullGlobPath("controllers"));

for (let i = 0; i < (controllers || []).length; i++) {
const module = importFresh(controllers[i]);
const name = this.normalizePath(controllers[i]);
const fullName = `${name}.${controllers[i].split(".").pop()}`;
try {
const module = importFresh(controllers[i]);
const name = this.normalizePath(controllers[i]);
const fullName = `${name}.${controllers[i].split(".").pop()}`;

logger.debug(`%p%P %s`, 3, 1, Config.relPath("controllers", fullName));
logger.debug(`%p%P %s`, 3, 1, Config.relPath("controllers", fullName));

try {
this.instantiate(module, name, this.getModuleKey(module));
} catch (e) {
logger.warn(
Expand All @@ -57,11 +57,17 @@ export class Controllers {
if (path in this.controllers) {
logger.debug(`%P Invoking controller at ${path}`, 2);

if (this.controllers[path] instanceof ControllerAbstract) {
return (<ControllerAbstract>this.controllers[path]).handle(req);
try {
if (this.controllers[path] instanceof ControllerAbstract) {
return (<ControllerAbstract>this.controllers[path]).handle(req);
}
return (<ControllerFunction>this.controllers[path])(req, res);
} catch(e) {
logger.error("%p%P Error while executing controller %s call", 3, 0, path)
logger.error("%O", e)
}
return (<ControllerFunction>this.controllers[path])(req, res);
}

return {};
}

Expand Down
4 changes: 4 additions & 0 deletions src/server/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class DataManager {
throw new UnregistredDataAdapterException(name);
}

static getDefault(): string | undefined {
return this.default
}

static setDefault(name: string): void {
this.default = name;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/json-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const createJsonRouter = (
return jsonDb.db;
}

const jsonDbConfig = Config.value<string>("jsonDb");
const jsonDbConfig = Config.get<string>("jsonDb");

if (jsonDbConfig.endsWith(".json")) {
logger.debug(
Expand Down

0 comments on commit f3f46a5

Please sign in to comment.