Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/commands/add-cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default class AddCert extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export default class Config extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/create-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export default class CreateFolder extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/delete-permanently-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class DeletePermanentlyFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/delete-permanently-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class DeletePermanentlyFolder extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/download-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export default class DownloadFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export default class List extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export default class Login extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default class Logout extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default class Logs extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/move-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default class MoveFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/move-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default class MoveFolder extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/rename-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default class RenameFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/rename-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class RenameFolder extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/trash-clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default class TrashClear extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/trash-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default class TrashFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/trash-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default class TrashFolder extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/trash-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export default class TrashList extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/trash-restore-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default class TrashRestoreFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/trash-restore-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default class TrashRestoreFolder extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default class UploadFile extends Command {
}
}
} catch (error) {
ErrorUtils.report(this.error.bind(this), error, { command: this.id });
ErrorUtils.report(error, { command: this.id });
}

progressBar?.update(100);
Expand All @@ -184,7 +184,6 @@ export default class UploadFile extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/webdav-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export default class WebDAVConfig extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/webdav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export default class Webdav extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/commands/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class Whoami extends Command {
error,
command: this.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag: flags['json'],
});
this.exit(1);
Expand Down
1 change: 0 additions & 1 deletion src/hooks/prerun/auth_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const hook: Hook<'prerun'> = async function (opts) {
error: err,
command: Command.id,
logReporter: this.log.bind(this),
errorReporter: this.error.bind(this),
jsonFlag,
});
opts.context.exit(1);
Expand Down
4 changes: 1 addition & 3 deletions src/utils/cli.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,12 @@ export class CLIUtils {
static readonly catchError = ({
error,
logReporter,
errorReporter,
command,
jsonFlag,
}: {
error: Error;
command?: string;
logReporter: (message: string) => void;
errorReporter: (message: string) => void;
jsonFlag?: boolean;
}) => {
let message;
Expand All @@ -196,7 +194,7 @@ export class CLIUtils {
if (jsonFlag) {
CLIUtils.consoleLog(JSON.stringify({ success: false, message }));
} else {
ErrorUtils.report(errorReporter, error, { command });
ErrorUtils.report(error, { command });
CLIUtils.error(logReporter, message);
}
};
Expand Down
15 changes: 11 additions & 4 deletions src/utils/errors.utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { logger } from './logger.utils';
import { types } from 'node:util';

export function isError(error: unknown): error is Error {
return types.isNativeError(error);
}

export class ErrorUtils {
static report(reporter: (error: string) => void, error: unknown, props: Record<string, unknown> = {}) {
if (error instanceof Error) {
reporter(
static report(error: unknown, props: Record<string, unknown> = {}) {
if (isError(error)) {
logger.error(
`[REPORTED_ERROR]: ${error.message}\nProperties => ${JSON.stringify(props, null, 2)}\nStack => ${error.stack}`,
);
} else {
reporter(`[REPORTED_ERROR]: ${JSON.stringify(error)}\nProperties => ${JSON.stringify(props, null, 2)}\n`);
logger.error(`[REPORTED_ERROR]: ${JSON.stringify(error)}\nProperties => ${JSON.stringify(props, null, 2)}\n`);
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/utils/logger.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,3 @@ export const webdavLogger = winston.createLogger({
}),
],
});

if (process.env.NODE_ENV !== 'production') {
webdavLogger.add(
new winston.transports.Console({
format: winston.format.simple(),
}),
);
}

if (process.env.NODE_ENV !== 'production') {
logger.add(
new winston.transports.Console({
format: winston.format.simple(),
}),
);
}
13 changes: 8 additions & 5 deletions src/webdav/middewares/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SdkManager } from '../../services/sdk-manager.service';
import { AuthService } from '../../services/auth.service';
import { webdavLogger } from '../../utils/logger.utils';
import { XMLUtils } from '../../utils/xml.utils';
import { isError } from '../../utils/errors.utils';

export const AuthMiddleware = (authService: AuthService): RequestHandler => {
return (_, res, next) => {
Expand All @@ -13,12 +14,14 @@ export const AuthMiddleware = (authService: AuthService): RequestHandler => {
next();
} catch (error) {
let message = 'Authentication required to access this resource.';
if ('message' in (error as Error) && (error as Error).message.trim().length > 0) {
message = (error as Error).message;
if (isError(error)) {
message = error.message;
if (error.stack) {
webdavLogger.error(`Error from AuthMiddleware: ${message}\nStack: ${error.stack}`);
} else {
webdavLogger.error(`Error from AuthMiddleware: ${message}`);
}
}

webdavLogger.error('Error from AuthMiddleware: ' + message);

const errorBodyXML = XMLUtils.toWebDavXML(
{
[XMLUtils.addDefaultNamespace('responsedescription')]: message,
Expand Down
11 changes: 9 additions & 2 deletions src/webdav/middewares/errors.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { ErrorRequestHandler } from 'express';
import { webdavLogger } from '../../utils/logger.utils';
import { XMLUtils } from '../../utils/xml.utils';
import { isError } from '../../utils/errors.utils';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const ErrorHandlingMiddleware: ErrorRequestHandler = (err, req, res, _) => {
webdavLogger.error(`[ERROR MIDDLEWARE] [${req.method.toUpperCase()} - ${req.url}]`, err);
const message = isError(err) ? err.message : 'Something went wrong';

if (isError(err) && err.stack) {
webdavLogger.error(`[ERROR MIDDLEWARE] [${req.method.toUpperCase()} - ${req.url}] ${message}\nStack: ${err.stack}`);
} else {
webdavLogger.error(`[ERROR MIDDLEWARE] [${req.method.toUpperCase()} - ${req.url}] ${message}`);
}

const errorBodyXML = XMLUtils.toWebDavXML(
{
[XMLUtils.addDefaultNamespace('responsedescription')]: 'message' in err ? err.message : 'Something went wrong',
[XMLUtils.addDefaultNamespace('responsedescription')]: message,
},
{},
'error',
Expand Down
15 changes: 12 additions & 3 deletions test/commands/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import Login from '../../src/commands/login';
import { AuthService } from '../../src/services/auth.service';
import { CLIUtils, NoFlagProvidedError } from '../../src/utils/cli.utils';

vi.mock('../../src/utils/logger.utils', () => ({
logger: {
error: vi.fn(),
},
}));

describe('Login Command', () => {
beforeEach(() => {
vi.restoreAllMocks();
Expand All @@ -26,7 +32,8 @@ describe('Login Command', () => {
await Login.run(['--non-interactive', `--password="${UserLoginFixture.password}"`]);
fail('Expected function to throw an error, but it did not.');
} catch (error) {
expect((error as Error).message).to.contain(new NoFlagProvidedError('email').message);
expect((error as Error).message).to.contain('EEXIT: 1');
expect((error as Error & { oclif?: { exit: number } }).oclif?.exit).to.equal(1);
}

expect(getValueFromFlagsSpy).toHaveBeenCalledOnce();
Expand All @@ -50,7 +57,8 @@ describe('Login Command', () => {
await Login.run(['--non-interactive', `--email="${UserLoginFixture.email}"`]);
fail('Expected function to throw an error, but it did not.');
} catch (error) {
expect((error as Error).message).to.contain(new NoFlagProvidedError('password').message);
expect((error as Error).message).to.contain('EEXIT: 1');
expect((error as Error & { oclif?: { exit: number } }).oclif?.exit).to.equal(1);
}

expect(getValueFromFlagsSpy).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -78,7 +86,8 @@ describe('Login Command', () => {
]);
fail('Expected function to throw an error, but it did not.');
} catch (error) {
expect((error as Error).message).to.contain(new NoFlagProvidedError('twofactor').message);
expect((error as Error).message).to.contain('EEXIT: 1');
expect((error as Error & { oclif?: { exit: number } }).oclif?.exit).to.equal(1);
}

expect(getValueFromFlagsSpy).toHaveBeenCalledTimes(3);
Expand Down
Loading