Skip to content

Commit

Permalink
Merge pull request #2334 from CAFECA-IO/feature/adjust_loggerError
Browse files Browse the repository at this point in the history
Feature/adjust logger error
  • Loading branch information
Luphia authored Sep 11, 2024
2 parents 14f1abe + d43160a commit 3b03047
Show file tree
Hide file tree
Showing 40 changed files with 568 additions and 205 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ next-env.d.ts
# backup
*.backup
pnpm-lock.yaml

# logs
/log/*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iSunFA",
"version": "0.8.1+6",
"version": "0.8.1+7",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
1 change: 0 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ main()
})
.catch(async () => {
// Info (20240316 - Murky) - disconnect prisma
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
await prisma.$disconnect();
process.exit(1);
});
9 changes: 5 additions & 4 deletions scripts/update_version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { execSync } from 'child_process';
import { loggerError } from '@/lib/utils/logger_back';

// Info: (20240701 - Jacky) Function to get the last committed version of package.json
function getLastCommittedVersion(): string | null {
Expand Down Expand Up @@ -28,7 +29,8 @@ try {
packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
packageJson = JSON.parse(packageJsonContent);
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(0, 'Package.json parsed failed', error as Error);
logError.error('Retreive packageJson in update_version.ts failed');
process.exit(1); // Info: (20240701 - Jacky) Exit the process with an error code
}

Expand Down Expand Up @@ -62,8 +64,7 @@ try {
// Info:(20240730 - Jacky) - Add last line to prevent EOF error
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(0, 'Package.json write failed', error as Error);
logError.error('Update(Write) packageJson into packageJsonPath in update_version.ts failed');
process.exit(1); // Info: (20240701 - Jacky) Exit the process with an error code
}

// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
4 changes: 2 additions & 2 deletions src/components/journal_item/journal_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ const JournalItem = ({
};

const debitItem = lineItems
? (lineItems.filter((item) => item.debit)[0] ?? defaultItem)
? lineItems.filter((item) => item.debit)[0] ?? defaultItem
: defaultItem;
const debit = {
account: debitItem.account,
amount: numberWithCommas(debitItem.amount),
};

const creditItem = lineItems
? (lineItems.filter((item) => !item.debit)[0] ?? defaultItem)
? lineItems.filter((item) => !item.debit)[0] ?? defaultItem
: defaultItem;
const credit = {
account: creditItem.account,
Expand Down
2 changes: 1 addition & 1 deletion src/components/journal_list_body/journal_list_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const JournalListBody = () => {
endDate: !(period ?? filteredPeriod).endTimeStamp
? undefined
: (period ?? filteredPeriod).endTimeStamp,
searchQuery: !(searchString ?? search) ? undefined : (searchString ?? search),
searchQuery: !(searchString ?? search) ? undefined : searchString ?? search,
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/nav_bar/nav_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ const NavBar = () => {
</button>
<div className="mt-3 flex justify-center gap-0 px-16">
<div className="my-auto text-base font-semibold leading-6 tracking-normal text-button-text-secondary">
{signedIn ? (username ?? DEFAULT_DISPLAYED_USER_NAME) : ''}
{signedIn ? username ?? DEFAULT_DISPLAYED_USER_NAME : ''}
</div>
{/* Info: (20240809 - Shirley) edit name button */}
<button
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/modal_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const ModalProvider = ({ children }: ModalProviderProps) => {
const position = toastPosition ?? ToastPosition.TOP_CENTER; // Info:(20240513 - Julian) default position 'top-center'

// Info:(20240513 - Julian) 如果 closeable 為 false,則 autoClose、closeOnClick、draggable 都會被設為 false
const autoClose = closeable ? (isAutoClose ?? 5000) : false; // Info:(20240513 - Julian) default autoClose 5000ms
const autoClose = closeable ? isAutoClose ?? 5000 : false; // Info:(20240513 - Julian) default autoClose 5000ms

const closeOnClick = closeable; // Info:(20240513 - Julian) default closeOnClick true
const draggable = closeable; // Info:(20240513 - Julian) default draggable true
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/user_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export const UserProvider = ({ children }: { children: React.ReactNode }) => {

const res = await selectCompanyAPI({
params: {
companyId: !company && !isPublic ? -1 : (company?.id ?? FREE_COMPANY_ID),
companyId: !company && !isPublic ? -1 : company?.id ?? FREE_COMPANY_ID,
},
});

Expand Down
2 changes: 0 additions & 2 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Info: (20240812 - Murky) All code in register will be run when "Starting..." is printed in the console.
*/
export async function register() {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger

// Info: (20240812 - Murky) Node module using in this file need to be dynamically imported, it must be inside "if" statement
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { createFileFoldersIfNotExists } = await import('@/lib/utils/file');
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import {
decryptFile,
getPrivateKeyByCompany,
} from '@/lib/utils/crypto';
import logger from '@/lib/utils/logger_back';
import loggerBack, { loggerError } from '@/lib/utils/logger_back';
import { STATUS_MESSAGE } from '@/constants/status_code';

export async function createFileFoldersIfNotExists(): Promise<void> {
UPLOAD_IMAGE_FOLDERS_TO_CREATE_WHEN_START_SERVER.map(async (folder) => {
try {
await fs.mkdir(folder, { recursive: true });
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(0, 'createFileFoldersIfNotExists failed', error as Error);
logError.error('Func. createFileFoldersIfNotExists in file.ts failed');
}
});
CRYPTO_FOLDER_PATH.map(async (folder) => {
Expand Down Expand Up @@ -84,7 +84,7 @@ export async function decryptImageFile({
const privateKey = await getPrivateKeyByCompany(companyId);

if (!privateKey) {
logger.error(`Private key not found in decryptImageFile in image/[imageId]: ${file.id}`);
loggerBack.error(`Private key not found in decryptImageFile in image/[imageId]: ${file.id}`);
throw new Error(STATUS_MESSAGE.FORBIDDEN);
}
const ivUint8Array = bufferToUint8Array(iv);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/generate_user_icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
generateSavePath,
uploadFileToGoogleCloud,
} from '@/lib/utils/google_image_upload';
import logger from '@/lib/utils/logger_back';
import loggerBack from '@/lib/utils/logger_back';

function isChinese(name: string): boolean {
return /[\u3400-\u9FBF]/.test(name);
Expand Down Expand Up @@ -102,7 +102,7 @@ export async function generateIcon(name: string) {
mimeType = mimeAndSize.mimeType;
size = mimeAndSize.size;
} catch (error) {
logger.error(error, 'Error happened in generateIcon in generate_user_icon.ts');
loggerBack.error(error, 'Error happened in generateIcon in generate_user_icon.ts');
}
return {
iconUrl,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/utils/google_image_upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from 'path';
import fs from 'fs/promises';
import { File } from 'formidable';
import { FileFolder, getFileFolder } from '@/constants/file';
import { loggerError } from '@/lib/utils/logger_back';

// Info: (20240604 - Murky) if process.env is not set, the error will stop all process, error can't be caught
export const googleStorage = new Storage({
Expand Down Expand Up @@ -63,7 +64,8 @@ export async function uploadFileToGoogleCloud(
await file.makePublic();
url = `${GOOGLE_STORAGE_BUCKET_URL}${destFileName}`;
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(0, 'uploadFileToGoogleCloud failed', error as Error);
logError.error('Func. uploadFileToGoogleCloud in google_image_upload.ts failed');
}
return url;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/parse_image_form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import { NextApiRequest } from 'next';
import { FORMIDABLE_OPTIONS } from '@/constants/config';
import { FileFolder, getFileFolder } from '@/constants/file';
import logger from '@/lib/utils/logger_back';
import loggerBack from '@/lib/utils/logger_back';

export const parseForm = async (
req: NextApiRequest,
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function readFile(baseFolder: string, fileName?: string): Promise<B
try {
fileBuffer = await fs.readFile(filePath);
} catch (error) {
logger.error(error, `Error in readFile: ${filePath}`);
loggerBack.error(error, `Error in readFile: ${filePath}`);
}
return fileBuffer;
}
Expand Down
50 changes: 43 additions & 7 deletions src/lib/utils/repo/account.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Account, Prisma } from '@prisma/client';
import { DEFAULT_PAGE_NUMBER } from '@/constants/display';
import { ReportSheetAccountTypeMap, ReportSheetType } from '@/constants/report';
import { SortOrder } from '@/constants/sort';
import { loggerError } from '@/lib/utils/logger_back';

export async function findManyAccountsInPrisma({
companyId,
Expand Down Expand Up @@ -90,7 +91,12 @@ export async function findManyAccountsInPrisma({
try {
totalCount = await prisma.account.count({ where });
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Count tototal count of account in findManyAccountsInPrisma failed',
error as Error
);
logError.error('Prisma count account in account.repo.ts failed');
}

const totalPage = Math.ceil(totalCount / limit);
Expand All @@ -109,7 +115,12 @@ export async function findManyAccountsInPrisma({
try {
accounts = await prisma.account.findMany(findManyArgs);
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Find many accounts in findManyAccountsInPrisma failed',
error as Error
);
logError.error('Prisma find many accounts in account.repo.ts failed');
}

const hasNextPage = accounts.length > limit;
Expand Down Expand Up @@ -151,7 +162,12 @@ export async function findFirstAccountInPrisma(accountId: number, companyId: num
},
});
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Find first account in findFirstAccountInPrisma failed',
error as Error
);
logError.error('Prisma find first account in account.repo.ts failed');
}

return account;
Expand All @@ -174,7 +190,12 @@ export async function updateAccountInPrisma(
},
});
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Update account in updateAccountInPrisma failed',
error as Error
);
logError.error('Prisma update account in account.repo.ts failed');
}

return account;
Expand All @@ -195,7 +216,12 @@ export async function softDeleteAccountInPrisma(accountIdNumber: number, company
},
});
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Soft delete account in softDeleteAccountInPrisma failed',
error as Error
);
logError.error('Prisma soft delete account in account.repo.ts failed');
}
return account;
}
Expand All @@ -212,7 +238,12 @@ export async function findLatestSubAccountInPrisma(parentAccount: Account) {
},
});
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Find latest sub account in findLatestSubAccountInPrisma failed',
error as Error
);
logError.error('Prisma find latest sub account in account.repo.ts failed');
}
return latestSubAccount;
}
Expand Down Expand Up @@ -249,7 +280,12 @@ export async function fuzzySearchAccountByName(name: string) {
`;
[account] = accounts;
} catch (error) {
// Todo: (20240822 - Anna): [Beta] feat. Murky - 使用 logger
const logError = loggerError(
0,
'Fuzzy search account by name in fuzzySearchAccountByName failed',
error as Error
);
logError.error('Prisma fuzzy search account by name in account.repo.ts failed');
}

return account;
Expand Down
16 changes: 8 additions & 8 deletions src/lib/utils/repo/file.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import prisma from '@/client';
import { FileDatabaseConnectionType, FileFolder } from '@/constants/file';
import { Prisma, File } from '@prisma/client';
import { getTimestampNow } from '@/lib/utils/common';
import logger from '@/lib/utils/logger_back';
import loggerBack from '@/lib/utils/logger_back';

/**
* Info: (20240830 - Murky)
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function createFile({
data: fileData,
});
} catch (error) {
logger.error(error, 'Error happened in createFile in file.repo.ts');
loggerBack.error(error, 'Error happened in createFile in file.repo.ts');
}
return file;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function connectFileById(
data: connectData,
});
} catch (error) {
logger.error(error, 'Error happened in connectFileById in file.repo.ts');
loggerBack.error(error, 'Error happened in connectFileById in file.repo.ts');
}
return file;
}
Expand All @@ -112,7 +112,7 @@ export async function findFileById(fileId: number): Promise<File | null> {
},
});
} catch (error) {
logger.error(error, 'Error happened in findFileById in file.repo.ts');
loggerBack.error(error, 'Error happened in findFileById in file.repo.ts');
}
return file;
}
Expand All @@ -127,7 +127,7 @@ export async function findFileInDBByName(name: string): Promise<File | null> {
},
});
} catch (error) {
logger.error(error, 'Error happened in findFileByName in file.repo.ts');
loggerBack.error(error, 'Error happened in findFileByName in file.repo.ts');
}
return file;
}
Expand All @@ -151,7 +151,7 @@ export async function deleteFileById(fileId: number) {
data,
});
} catch (error) {
logger.error(error, 'Error happened in deleteFileById in file.repo.ts');
loggerBack.error(error, 'Error happened in deleteFileById in file.repo.ts');
}

return file;
Expand All @@ -177,7 +177,7 @@ export async function deleteFileByName(name: string) {
data,
});
} catch (error) {
logger.error(error, 'Error happened in deleteFileByName in file.repo.ts');
loggerBack.error(error, 'Error happened in deleteFileByName in file.repo.ts');
}

return file;
Expand All @@ -194,7 +194,7 @@ export async function deleteFileByIdForTesting(fileId: number) {
where,
});
} catch (error) {
logger.error(error, 'Error happened in deleteFileByIdForTest in file.repo.ts');
loggerBack.error(error, 'Error happened in deleteFileByIdForTest in file.repo.ts');
}

return file;
Expand Down
Loading

0 comments on commit 3b03047

Please sign in to comment.