Enhance socket management and error handling in template events
- Increased the maximum HTTP buffer size for the Socket.IO server to improve data handling capabilities. - Added error handling in `handlePreviewTemplateEvent` and `handleRenderTemplatePDFEvent` methods to log errors and return error messages through callbacks. - Implemented a check for unknown object types in the `TemplateManager` to prevent processing errors and return appropriate error messages.
This commit is contained in:
parent
00198b8199
commit
8c445a7392
@ -23,6 +23,7 @@ export class SocketManager {
|
|||||||
// Create Socket.IO server - CORS applies to HTTP long-polling transport
|
// Create Socket.IO server - CORS applies to HTTP long-polling transport
|
||||||
const allowedOrigins = config.server.corsOrigins || ['*'];
|
const allowedOrigins = config.server.corsOrigins || ['*'];
|
||||||
const io = new Server(server, {
|
const io = new Server(server, {
|
||||||
|
maxHttpBufferSize: 1e8,
|
||||||
cors: {
|
cors: {
|
||||||
origin: (origin, callback) => {
|
origin: (origin, callback) => {
|
||||||
// Allow requests with no origin (e.g. same-origin, Postman, native apps)
|
// Allow requests with no origin (e.g. same-origin, Postman, native apps)
|
||||||
|
|||||||
@ -306,23 +306,41 @@ export class SocketUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handlePreviewTemplateEvent(data, callback) {
|
async handlePreviewTemplateEvent(data, callback) {
|
||||||
const result = await this.templateManager.renderTemplate(
|
try {
|
||||||
data._id,
|
const result = await this.templateManager.renderTemplate(
|
||||||
data.content,
|
data._id,
|
||||||
data.testObject,
|
data.content,
|
||||||
data.scale
|
data.testObject,
|
||||||
);
|
data.scale
|
||||||
callback(result);
|
);
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
callback(result);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Preview template event error:', err);
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
callback({ error: err.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleRenderTemplatePDFEvent(data, callback) {
|
async handleRenderTemplatePDFEvent(data, callback) {
|
||||||
const result = await this.templateManager.renderPDF(
|
try {
|
||||||
data._id,
|
const result = await this.templateManager.renderPDF(
|
||||||
data.content,
|
data._id,
|
||||||
data.object,
|
data.content,
|
||||||
1
|
data.object,
|
||||||
);
|
1
|
||||||
callback(result);
|
);
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
callback(result);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Render template PDF event error:', err);
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
callback({ error: err.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
async handleGenerateHostOtpEvent(data, callback) {
|
async handleGenerateHostOtpEvent(data, callback) {
|
||||||
const result = await generateHostOTP(data._id);
|
const result = await generateHostOTP(data._id);
|
||||||
|
|||||||
@ -407,6 +407,9 @@ export class TemplateManager {
|
|||||||
} else {
|
} else {
|
||||||
const objectType = documentTemplate?.objectType;
|
const objectType = documentTemplate?.objectType;
|
||||||
const model = getModelByName(objectType);
|
const model = getModelByName(objectType);
|
||||||
|
if (model == null) {
|
||||||
|
return { error: `Unknown object type: ${objectType}` };
|
||||||
|
}
|
||||||
const defaultKeys = Object.keys(model.schema.obj);
|
const defaultKeys = Object.keys(model.schema.obj);
|
||||||
const defaultValues = {};
|
const defaultValues = {};
|
||||||
for (const key of defaultKeys) {
|
for (const key of defaultKeys) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user