In my series on automated CI/CD for a Power Apps code app, the errors from the app become issues that an AI code agent fixes. This guide sets up the first step, where the app writes its errors to a Dataverse table.
The series has two alternatives for this step: a Dataverse table in this guide, or Application Insights. The overview of the series shows both.
What it does:
- The app catches the error. Two event listeners pick up the uncaught errors and the unhandled promises, and a small function called
logIfFailedpicks up the failed calls. - The app writes a row to a table called App Error, as the signed-in user. The security role of the user has to allow that first.
- Dataverse stores the row. You find it in the table within seconds, and a job deletes the old rows.
You need a solution where you can create a table and a code app that reads from Dataverse through a generated service (in my app Dept_deptcasesService). The app is already published to your environment with the Power Apps CLI (pa), as in Microsoft’s quickstart. Steps 8 and 9 need the System Administrator role in the environment.
Step 1. Create the table
Open the Power Apps maker portal and go to Solutions. Open your solution and select New > Table > Table (advanced properties).
- On the Properties tab, enter the display name
App Error. The plural name fills in by itself. - On the Primary column tab, enter the display name
Message. - Select Save.



The Message column holds 850 characters by default.
The code in step 4 cuts the message with .slice(0, 850), so that it fits the column. To store longer messages, raise Maximum character count under Advanced options (up to 4,000) and change 850 in the code to the same number.
Step 2. Add the columns
Open the table and select New > Column for each of these:
| Display name | Data type |
|---|---|
| Operation | Single line of text |
| Request ID | Single line of text |
| HTTP Status | Whole number |
| Details | Multiple lines of text, format Text |



I named the column HTTP Status because Dataverse already has a system column called Status.
Details holds the stack and takes 2,000 characters by default.
The code in step 4 cuts the stack with .slice(0, 2000), so that it fits the column. To store more of the stack, raise Maximum character count under Advanced options of the column (up to 1,048,576) and change 2000 in the code to the same number.

Dataverse adds Created By and Created On to the table. They give you who had the error and the time, and the app needs no code for them.
Step 3. Add the table to the code app
The code app can only write to a table that is added as a data source. Run this in the project folder, with the logical name of your table and the address of your environment:
npx pa app add data-source --connector dataverse --table dept_apperror --org-url https://<your-org>.crm.dynamics.com

dept_ is the prefix of my publisher, so your table has another name. Without --org-url the command stops and asks “Please provide the organization URL:”.
The command generates a model and a service for the table in src/generated (in my app Dept_apperrorsModel.ts and Dept_apperrorsService.ts) and adds the table to power.config.json.
Step 4. Create errorTable.ts
src/errorTable.ts is the only file you write yourself, and it has everything the app needs to store errors. Create it with this content:
import { Dept_apperrorsService } from './generated'
import type { IOperationResult } from '@microsoft/power-apps/data'
// Writes a row to the App Error table. Dataverse fills in who and when (Created By, Created On).
function logError(error: unknown, operation: string, status?: number, requestId?: string) {
const e = error instanceof Error ? error : new Error(String(error))
void Dept_apperrorsService.create({
// Record IDs are replaced, so the same error on two records gives the same message.
dept_message: e.message.replace(/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/gi, '{id}').slice(0, 850),
dept_operation: operation,
// The stack carries the app's full address on every line. Only the file name is kept.
dept_details: e.stack?.replace(/https?:\/\/\S*\//g, '').slice(0, 2000),
dept_httpstatus: status,
dept_requestid: requestId,
statecode: 0,
})
}
// Errors that no code caught, and promises that no code handled.
window.addEventListener('error', (event) => logError(event.error ?? event.message, 'uncaught error'))
window.addEventListener('unhandledrejection', (event) => logError(event.reason, 'rejected promise'))
// A failed Dataverse or connector call does not throw. It returns success: false,
// so the app has to send that error on itself.
export function logIfFailed<T>(result: IOperationResult<T>, operation: string) {
if (!result.success) {
const error = result.error as { message?: string; status?: number; requestId?: string } | undefined
logError(
new Error(readMessage(error?.message) ?? `${operation} failed`),
operation,
error?.status,
error?.requestId,
)
}
return result
}
// Dataverse sends its error as JSON text. The readable sentence is in error.message.
function readMessage(message?: string) {
try {
return JSON.parse(message ?? '').error.message as string
} catch {
return message
}
}

src/errorTable.ts is marked in the Explorer, and the whole file in the editor.logError writes the row with the generated service. It changes the error in these places before it writes:
- A record ID in the message becomes
{id}, so the same error on two records gives the same message. - The stack keeps only the file name. Every line of the stack has the full address of the app, with the IDs of the environment, the app and the tenant, and in the next posts the row becomes an issue.
.slice(0, 850)and.slice(0, 2000)cut the message and the stack to the length of the columns from step 1 and 2. If you raised Maximum character count there, change the numbers here to match.
The generated type requires statecode: 0, which sets the row to active.
The two window listeners catch the uncaught errors and the unhandled promises. logIfFailed takes the result of a call and a name for the operation, writes a row for a failed result and returns the result unchanged. Dataverse sends its error as a JSON text of several hundred characters, and readMessage picks the readable sentence out of it.
Step 5. Load errorTable.ts as the app starts
The listeners have to be in place before the app starts, so that they also catch an error from the first screen. Open src/main.tsx and add the import above the import of App:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './errorTable.ts'
import './index.css'
import App from './App.tsx'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)

src/main.tsx is marked in the Explorer, and the line import './errorTable.ts' in the editor.Step 6. Wrap the Dataverse calls
A failed Dataverse call gets a row only through logIfFailed, so wrap every call to a generated service. Open src/App.tsx and import the function:
import { logIfFailed } from './errorTable.ts'

src/App.tsx is marked in the Explorer, and the import in the editor.Put logIfFailed around each call. My app has a call that loads the cases and a call that creates a case:
async function load() {
setState('loading')
const result = logIfFailed(await Dept_deptcasesService.getAll(), 'load cases')
if (result.success && result.data) {
setCases(result.data)
setState('ready')
} else {
setError(result.error?.message ?? 'Dataverse returned no data.')
setState('error')
}
}

src/App.tsx is marked in the Explorer, and the function load with logIfFailed around its call in the editor.async function addCase() {
if (!title.trim()) return
const result = logIfFailed(
await Dept_deptcasesService.create({ dept_name: title.trim(), statecode: 0 }),
'create case',
)
if (result.success) {
setTitle('')
await load()
} else {
setError(result.error?.message ?? 'Create failed.')
setState('error')
}
}

src/App.tsx is marked in the Explorer, and the function addCase with logIfFailed around its call in the editor.The second argument is a name you choose for the operation, and it ends up in the Operation column. The published code is minified (in my stored stacks logIfFailed was called Jr), so this name is how you see which call failed.
Step 7. Build and publish
Run these commands in the project folder:
npm run build
npx pa app push
npm run buildchecks the TypeScript code and builds the app into thedistfolder.npx pa app pushuploads thedistfolder to your environment as a new version of the code app.pais the Power Apps CLI from the npm package@microsoft/power-apps-cli, and it reads the environment and the app frompower.config.json.
The published app wrote rows without any change to the Content Security Policy of the environment.
Step 8. Give the users the Create privilege on the table
The app writes the row as the signed-in user, so the security role your users have needs the Create privilege on App Error. Without it Dataverse refuses the row, and the app stores nothing about that.
- Open the Power Platform admin center and select Manage > Environments. Select your environment and then Settings.
- Expand Users + permissions and select Security roles. Open the role your users have (in my environment Dept App User).
- Change the filter from Show only assigned tables to Show all tables and search for
App Error. With the first filter the search answers “No privileges matched the search text”, because the role has no privileges on the new table yet. - Set Create to User and Read to User.
- Select Save. The page answers “Privileges are updated!”.




User is the lowest level in the list. I also set Read, and I have not tested whether Create alone is enough.
Step 9. Add the clean-up job
Every error is a new row and nothing removes the old ones, so a recurring job has to. Microsoft says you need System Administrator permissions for it.
- In the Settings of the environment, expand Data management and select Bulk deletion. A new browser tab opens with the list All Bulk Deletion System Jobs.
- Select New Job and enter a title, for example
Delete App Errors older than 90 days. - Tick Run job after every and choose 7 days.
- In Look for Entity, type
App Errorsand press Enter. - The filter has a row that says Status Equals Active. Change that row to Created On, Older than x days and
90. - Select Save and close.



Switch the list to the view Recurring Bulk Deletion System Jobs. The job is there with the status reason Waiting and its next run a week ahead.

Step 10. Check that it works
Add a temporary button that throws an error. Put it under the form in src/App.tsx:
<button
onClick={() => {
throw new Error('Test: error in a click handler')
}}
>
Throw error
</button>

src/App.tsx is marked in the Explorer, and the temporary button in the editor.- Run
npm run buildandnpx pa app push. Open the app and select Throw error. - Open the App Error table in the maker portal. Check that the table has a new row with the message “Test: error in a click handler” and your name in Created By. My rows were there within seconds.
- Remove the button. Build and publish again.


What a stored row contains
- Message: the error message, with
{id}in place of a record ID. - Operation: the name you gave the call, or
uncaught error/rejected promise. - HTTP Status and Request ID: filled in for a failed call and empty for the other errors.
- Details: the stack with file names only. In the published app that is the minified file, for example
index-<hash>.js:9:107203. - Created By and Created On: the signed-in user and the time.
This guide sets up the basics. For a production app you can also send the app version with each error, so you know which version had the error.
Posts in the series
Log the errors
Documentation and code
- Remove a large amount of specific, targeted data with bulk deletion (Microsoft documentation)
- The code for this guide (branch
feature/error-log-table)

Leave a Reply