Skip to content

Commit dd72866

Browse files
committed
Add cloudFunctionError
1 parent 09f18ff commit dd72866

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

spec/CloudCodeLogger.spec.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,39 @@ describe('Cloud Code Logger', () => {
183183
});
184184

185185
it('should log cloud function execution using the custom log level', async done => {
186+
Parse.Cloud.define('aFunction', () => {
187+
return 'it worked!';
188+
});
189+
190+
Parse.Cloud.define('bFunction', () => {
191+
throw new Error('Failed');
192+
});
193+
194+
await Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => {
195+
const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0];
196+
expect(log).toEqual('info');
197+
});
198+
186199
await reconfigureServer({
187200
silent: true,
188201
logLevels: {
189-
cloudFunctionRan: 'warn',
202+
cloudFunctionSuccess: 'warn',
203+
cloudFunctionError: 'info',
190204
},
191205
});
192206

193-
Parse.Cloud.define('aFunction', () => {
194-
return 'it worked!';
195-
});
196-
197207
spy = spyOn(Config.get('test').loggerController.adapter, 'log').and.callThrough();
198208

199-
Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => {
200-
const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0];
201-
expect(log).toEqual('warn');
209+
try {
210+
await Parse.Cloud.run('bFunction', { foo: 'bar' });
211+
throw new Error('bFunction should have failed');
212+
} catch {
213+
const log = spy.calls
214+
.allArgs()
215+
.find(log => log[1].startsWith('Failed running cloud function bFunction for '))?.[0];
216+
expect(log).toEqual('info');
202217
done();
203-
});
218+
}
204219
});
205220

206221
it('should log cloud function triggers using the custom log level', async () => {

src/Options/Definitions.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,14 @@ module.exports.AuthAdapter = {
993993
},
994994
};
995995
module.exports.LogLevels = {
996-
cloudFunctionRan: {
997-
env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_RAN',
998-
help: 'Log level used by the Cloud Code Functions. Default is `info`.',
996+
cloudFunctionError: {
997+
env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_ERROR',
998+
help: 'Log level used by the Cloud Code Functions on error. Default is `error`.',
999+
default: 'error',
1000+
},
1001+
cloudFunctionSuccess: {
1002+
env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_SUCCESS',
1003+
help: 'Log level used by the Cloud Code Functions on success. Default is `info`.',
9991004
default: 'info',
10001005
},
10011006
triggerAfter: {

src/Options/docs.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,12 @@ export interface LogLevels {
577577
:DEFAULT: error
578578
*/
579579
triggerBeforeError: ?string;
580-
/* Log level used by the Cloud Code Functions. Default is `info`.
580+
/* Log level used by the Cloud Code Functions on success. Default is `info`.
581581
:DEFAULT: info
582582
*/
583583
cloudFunctionSuccess: ?string;
584+
/* Log level used by the Cloud Code Functions on error. Default is `error`.
585+
:DEFAULT: error
586+
*/
587+
cloudFunctionError: ?string;
584588
}

src/Routers/FunctionsRouter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class FunctionsRouter extends PromiseRouter {
140140
result => {
141141
try {
142142
const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result));
143-
logger[req.config.logLevels.cloudFunctionRan](
143+
logger[req.config.logLevels.cloudFunctionSuccess](
144144
`Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Result: ${cleanResult}`,
145145
{
146146
functionName,
@@ -155,7 +155,7 @@ export class FunctionsRouter extends PromiseRouter {
155155
},
156156
error => {
157157
try {
158-
logger.error(
158+
logger[req.config.logLevels.cloudFunctionError](
159159
`Failed running cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Error: ` +
160160
JSON.stringify(error),
161161
{

0 commit comments

Comments
 (0)