Skip to content

Commit

Permalink
pipe format error to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
swk777 committed Sep 12, 2024
1 parent 34737ea commit 0a68c3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
28 changes: 19 additions & 9 deletions packages/api/server/ws.mts
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,27 @@ async function cellFormat(payload: CellFormatPayloadType) {
}
const result = await formatAndUpdateCodeCell(session, cellBeforeUpdate);
if (!result.success) {
return sendCellUpdateError(session, payload.cellId, result.errors);
}

const cell = result.cell as CodeCellType;
wss.broadcast(`session:${session.id}`, 'cell:output', {
cellId: payload.cellId,
output: { type: 'stderr', data: result.errors },
});
sendCellUpdateError(session, payload.cellId, [
{
message:
'An error occurred while formatting the code. Please check the "stderr" for more details.',
attribute: 'formatting',
},
]);
} else {
const cell = result.cell as CodeCellType;

wss.broadcast(`session:${session.id}`, 'cell:formatted', {
cellId: payload.cellId,
cell,
});
wss.broadcast(`session:${session.id}`, 'cell:formatted', {
cellId: payload.cellId,
cell,
});

refreshCodeCellDiagnostics(session, cell);
refreshCodeCellDiagnostics(session, cell);
}
}

async function cellUpdate(payload: CellUpdatePayloadType) {
Expand Down
10 changes: 5 additions & 5 deletions packages/api/session.mts
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ export async function formatCode(dir: string, fileName: string) {
const command = `npx prettier ${codeFilePath}`;

return new Promise((resolve, reject) => {
exec(command, async (error, stdout) => {
if (error) {
console.error(`exec error: ${error}`);
reject(error);
exec(command, async (_, stdout, stderr) => {
if (stderr) {
console.error(`exec error: ${stderr}`);
reject(stderr);
return;
}
resolve(stdout);
Expand All @@ -357,7 +357,7 @@ export async function formatAndUpdateCodeCell(session: SessionType, cell: CodeCe
} catch (error) {
return Promise.resolve({
success: false,
errors: [{ message: 'An error occurred formatting the code.', attribute: 'formatting' }],
errors: error,
} as UpdateResultType);
}
}
Expand Down

0 comments on commit 0a68c3d

Please sign in to comment.