fix(core.gbapp): Improvements in BASIC error handling.
This commit is contained in:
parent
aa04ba7992
commit
46f4cac29b
2 changed files with 154 additions and 29 deletions
|
@ -40,11 +40,12 @@ import urlJoin = require('url-join');
|
||||||
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
|
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
|
||||||
import { AzureDeployerService } from '../../azuredeployer.gbapp/services/AzureDeployerService';
|
import { AzureDeployerService } from '../../azuredeployer.gbapp/services/AzureDeployerService';
|
||||||
import { GBDeployer } from './GBDeployer';
|
import { GBDeployer } from './GBDeployer';
|
||||||
const MicrosoftGraph = require('@microsoft/microsoft-graph-client');
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
||||||
import { Messages } from '../strings';
|
import { Messages } from '../strings';
|
||||||
import { GBServer } from '../../../src/app';
|
import { GBServer } from '../../../src/app';
|
||||||
import { SecService } from '../../security.gbapp/services/SecService';
|
import { SecService } from '../../security.gbapp/services/SecService';
|
||||||
const request = require('request-promise-native');
|
const request = require('request-promise-native');
|
||||||
|
const MicrosoftGraph = require('@microsoft/microsoft-graph-client');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview General Bots server core.
|
* @fileoverview General Bots server core.
|
||||||
|
@ -69,12 +70,9 @@ class SysClass {
|
||||||
encoding: 'binary'
|
encoding: 'binary'
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await request(options);
|
const res = await request(options);
|
||||||
return Buffer.from(res, 'binary').toString();
|
return Buffer.from(res, 'binary').toString();
|
||||||
} catch (error) {
|
|
||||||
throw new Error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getRandomId() {
|
public async getRandomId() {
|
||||||
|
@ -117,7 +115,6 @@ class SysClass {
|
||||||
|
|
||||||
public async set(file: string, address: string, value: any): Promise<any> {
|
public async set(file: string, address: string, value: any): Promise<any> {
|
||||||
GBLog.info(`BASIC: Defining '${address}' in '${file}' to '${value}' (SET). `);
|
GBLog.info(`BASIC: Defining '${address}' in '${file}' to '${value}' (SET). `);
|
||||||
try {
|
|
||||||
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
||||||
|
|
||||||
let siteId = process.env.STORAGE_SITE_ID;
|
let siteId = process.env.STORAGE_SITE_ID;
|
||||||
|
@ -140,7 +137,7 @@ class SysClass {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!document || document.length === 0) {
|
if (!document || document.length === 0) {
|
||||||
throw `File '${file}' specified on save GBasic command SET not found. Check the .gbdata or the .gbdialog associated.`;
|
throw `File '${file}' specified on save GBasic command SET not found. Check the file extension (.xlsx) and the associated .gbdata/.gbdialog.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = { values: [[]] };
|
let body = { values: [[]] };
|
||||||
|
@ -151,15 +148,10 @@ class SysClass {
|
||||||
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='${address}')`
|
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='${address}')`
|
||||||
)
|
)
|
||||||
.patch(body);
|
.patch(body);
|
||||||
} catch (error) {
|
|
||||||
GBLog.error(`SET BASIC error: ${error.message}`);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async save(file: string, ...args): Promise<any> {
|
public async save(file: string, ...args): Promise<any> {
|
||||||
GBLog.info(`BASIC: Saving '${file}' (SAVE). Args: ${args.join(',')}.`);
|
GBLog.info(`BASIC: Saving '${file}' (SAVE). Args: ${args.join(',')}.`);
|
||||||
try {
|
|
||||||
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
||||||
|
|
||||||
let siteId = process.env.STORAGE_SITE_ID;
|
let siteId = process.env.STORAGE_SITE_ID;
|
||||||
|
@ -205,10 +197,6 @@ class SysClass {
|
||||||
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:DX2')`
|
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:DX2')`
|
||||||
)
|
)
|
||||||
.patch(body);
|
.patch(body);
|
||||||
} catch (error) {
|
|
||||||
GBLog.error(`SAVE BASIC error: ${error.message}`);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async get(file: string, address: string): Promise<any> {
|
public async get(file: string, address: string): Promise<any> {
|
||||||
|
@ -224,7 +212,6 @@ class SysClass {
|
||||||
const botId = this.min.instance.botId;
|
const botId = this.min.instance.botId;
|
||||||
const path = `/${botId}.gbai/${botId}.gbdata`;
|
const path = `/${botId}.gbai/${botId}.gbdata`;
|
||||||
|
|
||||||
try {
|
|
||||||
let res = await client
|
let res = await client
|
||||||
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
|
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
|
||||||
.get();
|
.get();
|
||||||
|
@ -250,9 +237,6 @@ class SysClass {
|
||||||
let val = results.text[0][0];
|
let val = results.text[0][0];
|
||||||
GBLog.info(`BASIC: Getting '${file}' (GET). Value= ${val}.`);
|
GBLog.info(`BASIC: Getting '${file}' (GET). Value= ${val}.`);
|
||||||
return val;
|
return val;
|
||||||
} catch (error) {
|
|
||||||
GBLog.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -269,7 +253,6 @@ class SysClass {
|
||||||
const botId = this.min.instance.botId;
|
const botId = this.min.instance.botId;
|
||||||
const path = `/${botId}.gbai/${botId}.gbdata`;
|
const path = `/${botId}.gbai/${botId}.gbdata`;
|
||||||
|
|
||||||
try {
|
|
||||||
let res = await client
|
let res = await client
|
||||||
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
|
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
|
||||||
.get();
|
.get();
|
||||||
|
@ -336,9 +319,151 @@ class SysClass {
|
||||||
GBLog.info(`BASIC: FIND multiple result count: ${array.length}.`);
|
GBLog.info(`BASIC: FIND multiple result count: ${array.length}.`);
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
}
|
||||||
GBLog.error(error);
|
|
||||||
}
|
/**
|
||||||
|
*
|
||||||
|
* folder = CREATE FOLDER "notes\01"
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param name Folder name containing tree separated by slash.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public async createFolder(name: string) {
|
||||||
|
|
||||||
|
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
||||||
|
let siteId = process.env.STORAGE_SITE_ID;
|
||||||
|
let libraryId = process.env.STORAGE_LIBRARY;
|
||||||
|
const botId = this.min.instance.botId;
|
||||||
|
const path = urlJoin(`/${botId}.gbai/${botId}.gbdata`, name);
|
||||||
|
|
||||||
|
return new Promise<any>((resolve, reject) => {
|
||||||
|
let client = MicrosoftGraph.Client.init({
|
||||||
|
authProvider: done => {
|
||||||
|
done(null, token);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const body = {
|
||||||
|
"name": name,
|
||||||
|
"folder": {},
|
||||||
|
"@microsoft.graph.conflictBehavior": "rename"
|
||||||
|
}
|
||||||
|
client.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${path}:/children`)
|
||||||
|
.post(body, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resolve(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* folder = CREATE FOLDER "notes\10"
|
||||||
|
* SHARE FOLDER folder, "nome@domain.com", "E-mail message"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public async shareFolder(folderReference, email: string, message: string) {
|
||||||
|
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
||||||
|
const driveId = folderReference.parentReference.driveId;
|
||||||
|
const itemId = folderReference.id;
|
||||||
|
|
||||||
|
return new Promise<string>((resolve, reject) => {
|
||||||
|
let client = MicrosoftGraph.Client.init({
|
||||||
|
authProvider: done => {
|
||||||
|
done(null, token);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const body =
|
||||||
|
{
|
||||||
|
"recipients": [
|
||||||
|
{
|
||||||
|
"email": email
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"message": message,
|
||||||
|
"requireSignIn": true,
|
||||||
|
"sendInvitation": true,
|
||||||
|
"roles": ["write"]
|
||||||
|
};
|
||||||
|
|
||||||
|
client.api(`https://graph.microsoft.com/v1.0/drives/${driveId}/items/${itemId}/invite`)
|
||||||
|
.post(body, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resolve(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async copyFile(src, dst) {
|
||||||
|
|
||||||
|
// let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
|
||||||
|
// let siteId = process.env.STORAGE_SITE_ID;
|
||||||
|
// let libraryId = process.env.STORAGE_LIBRARY;
|
||||||
|
// const botId = this.min.instance.botId;
|
||||||
|
// const path = urlJoin(`/${botId}.gbai/${botId}.gbdata`, name);
|
||||||
|
|
||||||
|
// let client = MicrosoftGraph.Client.init({
|
||||||
|
// authProvider: done => {
|
||||||
|
// done(null, token);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const body =
|
||||||
|
// {
|
||||||
|
// "parentReference": { driveId: gbaiDest.parentReference.driveId, id: gbaiDest.id },
|
||||||
|
// "name": `${botName}.${kind}`
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const packageName = `${templateName.split('.')[0]}.${kind}`;
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// const src = await client.api(
|
||||||
|
// `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${source}`)
|
||||||
|
// .get();
|
||||||
|
|
||||||
|
// return await client.api(
|
||||||
|
// `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${src.id}/copy`)
|
||||||
|
// .post(body);
|
||||||
|
|
||||||
|
// } catch (error) {
|
||||||
|
|
||||||
|
// if (error.code === "itemNotFound") {
|
||||||
|
|
||||||
|
// } else if (error.code === "nameAlreadyExists") {
|
||||||
|
|
||||||
|
// let src = await client.api(
|
||||||
|
// `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${templateName}/${packageName}:/children`)
|
||||||
|
// .get();
|
||||||
|
// const dstName = `${botName}.gbai/${botName}.${kind}`;
|
||||||
|
// let dst = await client.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${dstName}`)
|
||||||
|
// .get();
|
||||||
|
|
||||||
|
// await CollectionUtil.asyncForEach(src.value, async item => {
|
||||||
|
|
||||||
|
// const body =
|
||||||
|
// {
|
||||||
|
// "parentReference": { driveId: dst.parentReference.driveId, id: dst.id }
|
||||||
|
// }
|
||||||
|
// await client.api(
|
||||||
|
// `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${templateId}/drive/items/${item.id}/copy`)
|
||||||
|
// .post(body);
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// GBLog.error(error);
|
||||||
|
// throw error;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public generatePassword() {
|
public generatePassword() {
|
||||||
|
@ -368,7 +493,6 @@ class SysClass {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic function to call any REST API.
|
* Generic function to call any REST API.
|
||||||
*/
|
*/
|
||||||
|
@ -483,7 +607,7 @@ export class DialogClass {
|
||||||
public async isAffirmative(step, text) {
|
public async isAffirmative(step, text) {
|
||||||
return text.toLowerCase().match(Messages['pt-BR'].affirmative_sentences); // TODO: Dynamitize.
|
return text.toLowerCase().match(Messages['pt-BR'].affirmative_sentences); // TODO: Dynamitize.
|
||||||
}
|
}
|
||||||
|
|
||||||
public async exit(step) {
|
public async exit(step) {
|
||||||
await step.endDialog();
|
await step.endDialog();
|
||||||
}
|
}
|
||||||
|
|
|
@ -337,7 +337,7 @@ export class GBVMService extends GBService {
|
||||||
parsedCode = code.substring(pos, pos + match1.index);
|
parsedCode = code.substring(pos, pos + match1.index);
|
||||||
parsedCode += ``;
|
parsedCode += ``;
|
||||||
parsedCode += `const ${promiseName}= async (step, ${variable}) => {`;
|
parsedCode += `const ${promiseName}= async (step, ${variable}) => {`;
|
||||||
parsedCode += ` return new Promise(async (resolve) => {`;
|
parsedCode += ` return new Promise(async (resolve, reject) => { try {`;
|
||||||
|
|
||||||
// Skips old construction and point to the async block.
|
// Skips old construction and point to the async block.
|
||||||
|
|
||||||
|
@ -368,7 +368,8 @@ export class GBVMService extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedCode += code.substring(start + match1[0].length + 1, pos + match1[0].length);
|
parsedCode += code.substring(start + match1[0].length + 1, pos + match1[0].length);
|
||||||
parsedCode += '});\n';
|
|
||||||
|
parsedCode += '}catch(error){reject(error);}});\n';
|
||||||
parsedCode += '}\n';
|
parsedCode += '}\n';
|
||||||
|
|
||||||
|
|
||||||
|
@ -639,7 +640,7 @@ export class GBVMService extends GBService {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error in BASIC code: ${error}`);
|
GBLog.error(`Error in BASIC code: ${error}`);
|
||||||
const locale = step.context.activity.locale;
|
const locale = step.context.activity.locale;
|
||||||
min.conversationalService.sendText(min, step, Messages[locale].very_sorry_about_error);
|
await min.conversationalService.sendText(min, step, Messages[locale].very_sorry_about_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return await step.endDialog();
|
return await step.endDialog();
|
||||||
|
|
Loading…
Add table
Reference in a new issue