MSBA-272 · Lesson 11 of 12 · 19 min
Error handling
▶ Video coming soon A video version of this lesson is on the way.
Maribeth had a habit Signe liked: whenever she tested something, she wrote down every error message she saw, word for word, and what she thought it meant. Her list from the transcript work had eleven entries. Four were clear. Three were technical messages a clerk could not act on. Two came from a flow and said only that an action had failed. One said a fee could not be calculated without saying why, and one was a stack trace. Signe used the list to open a session on error handling, which she described as the part of extension work that users see most and developers think about least.
Two audiences for every error
An error has two readers. The user needs to know what went wrong in terms they understand and what to do next: correct a value, try again later, or contact someone. The developer or administrator needs to know exactly what failed, where and with what data, so they can fix it. Good error handling gives each reader what they need and does not give the user what the developer needs. A stack trace on a clerk's screen helps nobody, and a message that says "Something went wrong" leaves the developer with nothing.
Errors in synchronous plug-ins
In a synchronous plug-in, the way to report a problem to the user is to throw an InvalidPluginExecutionException with a clear message. Dataverse cancels the operation, rolls back the transaction, and shows the message in the app or returns it to the API caller. Any other exception type also cancels the operation, but the user sees a generic error with technical details, which is why Lumenvale's rule was that plug-ins throw InvalidPluginExecutionException for expected problems and let only genuinely unexpected failures surface as other exceptions.
if (rate == null)
{
local.Trace("No rate for method {0} on {1:yyyy-MM-dd}", method.Value, asOf);
throw new InvalidPluginExecutionException(
"The transcript fee for this delivery method is not set up for this date. " +
"Contact the registrar's office before saving the request.");
}Signe asked Maribeth to rewrite every message the plug-ins could show. Her rules were simple: say what happened in the user's words, say what to do, and leave out codes and internal names. "Choose a delivery method before saving" replaced "lcc_deliverymethod is null". "This request is already marked as Sent, so its fee cannot be waived" replaced "Invalid state transition". The technical detail went into a trace line next to the throw, where developers would find it.
A data error ends the transaction
Wiremu learned the next rule the hard way. His first version of the waiver logic created an audit row and then updated the request. During a test, the audit row creation failed because of a missing privilege. He had wrapped the call in a try block, logged the error and carried on to update the request, expecting the update to succeed without the audit row. Instead, the update failed with an error saying there was no active transaction.
Microsoft documents this behaviour. In a synchronous plug-in, when a call to the organization service fails, the database transaction is already rolled back, even if the plug-in catches the exception. Nothing the plug-in does afterwards in that transaction can succeed. The correct responses are to let the error propagate, or to catch it only to throw an InvalidPluginExecutionException with a better message. Trying to continue is never correct.
The design lesson was to check what could be checked before writing anything. The waiver logic now verified the request status and the caller's privilege first, threw a clear InvalidPluginExecutionException if either was wrong, and only then performed its writes. If a write still failed, the whole waiver was rolled back, which was exactly right: a half-waived fee would have been worse than none.
Errors in custom APIs
A custom API's main operation reports errors the same way, by throwing InvalidPluginExecutionException. The caller receives an error response with the message. For Web API callers the response has an HTTP error status, and the exception has constructor overloads that let a custom API choose a more specific status, such as a 400 for invalid input or a 403 for a failed business permission check. Signe asked Wiremu to use them in Calculate Transcript Fee and Waive Transcript Fee, so that integration developers could tell a bad request from a problem on the server without parsing messages.
She also asked for consistency. Every Lumenvale API returned the same kind of message for the same kind of problem: a missing required parameter, an unknown delivery method, a request in the wrong state. The custom API documentation page in the repository listed each error with its status and message, so the Request a Transcript app could show its own friendlier text if it wanted to.
Errors in asynchronous plug-ins
An asynchronous plug-in has no user waiting. If it throws, the system job records the failure, and the message never appears on anyone's screen. That makes it easy for failures to go unnoticed. Lesson 3 showed how to find them in the system jobs view; this session was about not needing to look.
For the AI classification step from lesson 5, Wiremu had already chosen to handle expected failures inside the plug-in: a capacity error or an unusable response set the Needs review flag and ended normally. Unexpected failures still threw, so they appeared as failed system jobs, and the monitoring alert from lesson 10 included failed jobs for Lumenvale's plug-ins. The principle was that every failure either became a visible state on the data, such as Needs review, or raised an alert to a person. Silent failure was not an option.
| Where the error happens | What the user sees | What the developer sees | Lumenvale practice |
|---|---|---|---|
| Synchronous plug-in, expected problem | The InvalidPluginExecutionException message | Trace lines in the error details and trace log | Plain message; technical detail in a trace |
| Synchronous plug-in, unexpected failure | A generic error with details | Exception and traces | Let it surface; fix the cause |
| Asynchronous plug-in | Nothing | A failed system job | Handle expected cases as data states; alert on failures |
| Custom API | The error from the calling app | Error response with status and message | Consistent statuses and documented messages |
| Flow calling a connector | Depends on the flow | Failed action in run history | Scopes with run-after settings and a notification |
Validating input early
Most errors a custom API returns should come from checking its input, not from something breaking halfway through. Wiremu reorganized both registrar APIs so that the main operation began with a validation block. It checked that required parameters were present, that the delivery method was one of the known values, that dates were within a sensible range, and for the waiver that the request existed and was in a state that allowed waiving. Only when every check passed did the code read rates or write anything.
Signe liked a further refinement for APIs with several parameters: collect every validation problem and report them together, rather than stopping at the first. An integration developer who sends a request with three mistakes would otherwise need three attempts to discover them. For Calculate Transcript Fee, with only three parameters, they kept it simple, but the pattern went into the base class for future APIs.
Messages people can act on and report
Ingrith Varsamis, the accessibility services coordinator, had reviewed Lumenvale apps with a screen reader in earlier projects, and her advice applied to error messages too. A message is read aloud in full, so it should be short, put the important part first, avoid capital letters for emphasis, and never rely on a colour or an icon to carry meaning. "Choose a delivery method before saving" works read aloud; a paragraph that starts with an error code does not.
Users also need a way to report a problem precisely. Clerks who called the service desk about an error were asked for the time it happened and the request number, which let developers find the matching rows in the plug-in trace log, because every Lumenvale trace began with the correlation ID and the row ID. Signe considered adding a short reference to every user-facing message, and decided against it for expected problems, where the message itself said what to do. For unexpected failures, the error details that the user could download already contained what the developers needed, and the service desk script asked for them.
The final version of Maribeth's list had eleven entries, all of them clear. She kept it in the solution's documentation as the reference for anyone writing a new message.
Calling external services from plug-ins
Plug-ins that call external services need extra care, because the service can be slow, unavailable or wrong. Microsoft's guidance is to set a timeout on outbound calls that is well within the two-minute limit, so a hanging service cannot consume the operation's whole budget. Wiremu set ten seconds for the one external call a registrar plug-in made. The code caught the timeout and other connection failures and turned them into an InvalidPluginExecutionException with a message telling the user to try again shortly, while tracing the host and the time taken.
Two other rules came from Microsoft's troubleshooting guidance. A plug-in must not start its own threads or tasks that outlive the execution, because an unhandled exception on such a thread can crash the sandbox worker and affect other plug-ins. And where a library offers only asynchronous methods, the plug-in should wait for them with GetAwaiter().GetResult(), which rethrows the original exception, rather than .Result, which wraps it and can deadlock.
Errors in flows and integrations
Many calls to Lumenvale's extensions came from Power Automate. The Parcelwise booking flow, for example, called the Parcelwise connector and then the Dataverse connector. MSBA-252 had covered flow error handling, and Signe repeated the parts that mattered here. Group related actions in a Scope, add a second scope configured to run after the first has failed or timed out, and in it record the failure on the transcript request and notify the registrar's team. Use the action's retry policy for transient errors such as 429 or 503, rather than building loops. And never let a flow end as Succeeded when its work was not done.
For integration code that called Dataverse directly, such as the spring import, the rules were those of lesson 10 plus one more: make operations idempotent where possible. If a request fails after Dataverse has processed it but before the client receives the response, the client cannot know whether it worked. Using alternate keys and upsert, so that sending the same row twice produces the same result, meant the import could safely retry anything it was unsure about.
Idempotency mattered for the courier booking too. If Create shipment timed out, the flow could not tell whether Parcelwise had booked the parcel before the connection dropped. A blind retry might book two couriers for one transcript. Parcelwise accepted a client reference on each shipment and rejected a second shipment with the same reference, so Wiremu set the reference to the transcript request number. A retry after an uncertain failure either created the shipment or received a clear duplicate response, which the flow treated as success after reading back the existing shipment. Signe noted that this only worked because the vendor supported it; for APIs without such a feature, the safer design is to check for an existing booking before retrying.
Worked problem
Handling a Parcelwise outage in the booking flow
During testing, the Parcelwise sandbox was down for an hour. The booking flow's Create shipment action failed with 503 errors, and the transcript requests stayed in the In progress status with no tracking number and no sign of a problem. How should the flow handle this?
Show solution
- Keep the default exponential retry policy on Create shipment, which handles brief outages without any extra design.
- Put Create shipment and the update of the request in a Try scope.
- Add a Catch scope set to run after the Try scope has failed or timed out. In it, set a Courier booking failed flag on the request and post a message to the registrar team's channel with the request number.
- Add a view in the Transcript Office app for requests with the flag set, so clerks can see and rebook them.
- End the flow with a Terminate action set to Failed in the Catch scope, so the run history shows the failure.
Answer: Retries absorb short outages; longer ones become a visible flag, a notification and a failed run, so no request is silently stuck.
Common mistakes
MistakeThrowing generic exceptions with technical text for expected problems.
InsteadThrow InvalidPluginExecutionException with a plain message and trace the details.
MistakeCatching a failed data operation in a synchronous plug-in and continuing.
InsteadLet it propagate or rethrow with a better message; the transaction is already gone.
MistakeLetting asynchronous failures fail silently.
InsteadTurn expected failures into visible data states and alert on the rest.
MistakeCalling external services without a timeout.
InsteadSet a short timeout and report failures clearly.
MistakeLetting a flow succeed when its work failed.
InsteadUse scopes, run-after settings and a failed termination.
Summary and key terms
- Every error serves two readers: the user, who needs a plain message and a next step, and the developer, who needs detail.
- Synchronous plug-ins report expected problems with InvalidPluginExecutionException, which cancels the operation and rolls back the transaction.
- After a failed data operation in a synchronous plug-in, the transaction is gone; do not try to continue.
- Custom APIs return consistent, documented errors with suitable HTTP statuses; asynchronous failures must become visible states or alerts.
- External calls need timeouts; flows need scopes and run-after handling; integrations should be idempotent.
- InvalidPluginExecutionException
- The exception a plug-in throws to cancel an operation with a message for the user.
- Rollback
- Undoing all changes made in a transaction when it fails.
- Idempotent
- Producing the same result whether an operation is performed once or several times.
- Scope
- A Power Automate container that groups actions and can be used for try and catch patterns.
- Run after
- A Power Automate setting that runs an action depending on whether earlier actions succeeded, failed, were skipped or timed out.
Practice problems
Work each problem on paper first, then reveal the answer to check.
What happens to the transaction when a data operation fails inside a synchronous plug-in?
Show answer
It is rolled back, even if the plug-in catches the exception.
Where does a failure of an asynchronous plug-in appear?
Show answer
In the system job record, not on the user's screen.
Why should integration operations be idempotent?
Show answer
So that retries after uncertain failures produce the same result.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.