-
-
Save arun12209/226c0c884d28796508048c817c5dc261 to your computer and use it in GitHub Desktop.
errorMessage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement,api } from 'lwc'; | |
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi'; | |
import { ShowToastEvent } from 'lightning/platformShowToastEvent'; | |
export default class ErrorMessage extends LightningElement { | |
subscription = {}; | |
@api channelName = '/event/Flow_Error__e'; | |
// connected callback : initialise | |
connectedCallback() { | |
// Register error listener | |
this.registerErrorListener(); | |
this.handlePESubscribe(); | |
} | |
handlePESubscribe() { | |
// Callback invoked whenever a new platfrom event message is created. | |
const thisReference = this; | |
const messageCallback = function(response) { | |
var obj = JSON.parse(JSON.stringify(response)); | |
//simplify the flow failure error message | |
let error_message = obj.data.payload.Error_Message_Text__c.substring(0, obj.data.payload.Error_Message_Text__c.indexOf("You can look up ExceptionCode values")); | |
console.log('Simplified error: '+ error_message); | |
//show toast message | |
const evt = new ShowToastEvent({ | |
title: 'Error !', | |
message: error_message, | |
variant: 'error', | |
mode:'sticky' | |
}); | |
thisReference.dispatchEvent(evt); | |
}; | |
// Invoke subscribe method of empApi. Pass reference to messageCallback | |
subscribe(this.channelName, -1, messageCallback).then(response => { | |
// Response contains the subscription information on subscribe call | |
console.log('Subscription request sent to: ', JSON.stringify(response.channel)); | |
this.subscription = response; | |
}); | |
} | |
/* If you want to unsubscribe use this channel. | |
handleUnsubscribe() { | |
// Invoke unsubscribe method of empApi | |
unsubscribe(this.subscription, response => { | |
console.log('unsubscribe method response: ', JSON.stringify(response)); | |
}); | |
} | |
*/ | |
// Error listner method | |
registerErrorListener() { | |
onError(error => { | |
// Error contains the server-side error | |
console.log('Error received: ', JSON.stringify(error)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment