Skip to content

Commit

Permalink
return 500 when lyra.yaml file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
amerharb committed Dec 14, 2023
1 parent fb4340e commit 82911ab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
27 changes: 18 additions & 9 deletions webapp/src/app/api/messages/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import { NextResponse } from 'next/server';
const REPO_PATH = process.env.REPO_PATH ?? envVarNotFound('REPO_PATH');

export async function GET() {
const config = await LyraConfig.readFromDir(REPO_PATH);
const msgAdapter = MessageAdapterFactory.createAdapter(config);
const messages = await msgAdapter.getMessages();
try {
const config = await LyraConfig.readFromDir(REPO_PATH);
const msgAdapter = MessageAdapterFactory.createAdapter(config);
const messages = await msgAdapter.getMessages();

// TODO: change data instruction to be a map of key to value, instead of object
// message id is the key, and value is an object with default and params
// example: { 'key1.key2.key3': { default: 'default text', params: [] }}
return NextResponse.json({
data: messages,
});
// TODO: change data instruction to be a map of key to value, instead of object
// message id is the key, and value is an object with default and params
// example: { 'key1.key2.key3': { default: 'default text', params: [] }}
return NextResponse.json({
data: messages,
});
} catch (e) {
return NextResponse.json(
// TODO: error message include e which contain a local path which is not of consumer interest
// remove it later, leave it for now for debugging purpose
{ message: `error reading messages error: ${e}` },
{ status: 500 }
);
}
}
6 changes: 6 additions & 0 deletions webapp/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export class LanguageNotFound extends Error {
super(`Language ${lang} not found`);
}
}

export class LyraConfigReadingError extends Error {
constructor(filename: string) {
super(`Error reading file: [${filename}]`);
}
}
38 changes: 23 additions & 15 deletions webapp/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'fs/promises';
import { logError } from '@/utils/util';
import { LyraConfigReadingError } from '@/errors';
import { parse } from 'yaml';
import path from 'path';
import { z } from 'zod';
Expand All @@ -8,7 +10,7 @@ export enum MessageKind {
YAML = 'yaml',
}

const KIND_BY_FORMAT_VALUE: Record<'yaml' | 'ts', MessageKind> = {
const KIND_BY_FORMAT_VALUE: Record<'ts' | 'yaml', MessageKind> = {
ts: MessageKind.TS,
yaml: MessageKind.YAML,
};
Expand All @@ -32,20 +34,26 @@ export default class LyraConfig {
private constructor(public readonly projects: LyraProjectConfig[]) {}

static async readFromDir(repoPath: string): Promise<LyraConfig> {
const ymlBuf = await fs.readFile(path.join(repoPath, 'lyra.yml'));
const configData = parse(ymlBuf.toString());

const parsed = configSchema.parse(configData);

return new LyraConfig(
parsed.projects.map((project) => {
return new LyraProjectConfig(
KIND_BY_FORMAT_VALUE[project.messages.format],
path.join(repoPath, project.path, project.messages.path),
path.join(repoPath, project.path, project.translations.path)
);
})
);
const filename = path.join(repoPath, 'lyra.yml');
try {
const ymlBuf = await fs.readFile(filename);
const configData = parse(ymlBuf.toString());

const parsed = configSchema.parse(configData);

return new LyraConfig(
parsed.projects.map((project) => {
return new LyraProjectConfig(
KIND_BY_FORMAT_VALUE[project.messages.format],
path.join(repoPath, project.path, project.messages.path),
path.join(repoPath, project.path, project.translations.path)
);
})
);
} catch (e) {
logError(`error reading ${filename} file`);
throw new LyraConfigReadingError(filename);
}
}
}

Expand Down

0 comments on commit 82911ab

Please sign in to comment.