generated from salesforcecli/lerna-template
-
Notifications
You must be signed in to change notification settings - Fork 15
fix: W-17774391 - create table for data import tree JSON Error #1190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc92bad
fix: create table for data import tree JSON Error
soridalac 2b24c88
chore: ci-rerun
soridalac ec9b254
fix: update the changes
soridalac a2f51eb
fix: remove extra function
soridalac a37ae87
fix: add error message
soridalac 0c0d27f
fix: update the error for json output
soridalac 95212ee
fix: preserve original error on invalid json payload
cristiand391 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,13 @@ | |
| * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| import { Messages } from '@salesforce/core'; | ||
| import { Messages, SfError } from '@salesforce/core'; | ||
| import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; | ||
| import { ensureString, isObject } from '@salesforce/ts-types'; | ||
| import { importFromPlan } from '../../../api/data/tree/importPlan.js'; | ||
| import { importFromFiles } from '../../../api/data/tree/importFiles.js'; | ||
| import { orgFlags } from '../../../flags.js'; | ||
| import type { ImportResult } from '../../../api/data/tree/importTypes.js'; | ||
| import type { ImportResult, TreeResponse } from '../../../api/data/tree/importTypes.js'; | ||
|
|
||
| Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); | ||
| const messages = Messages.loadMessages('@salesforce/plugin-data', 'tree.import'); | ||
|
|
@@ -49,19 +50,71 @@ export default class Import extends SfCommand<ImportResult[]> { | |
| const { flags } = await this.parse(Import); | ||
|
|
||
| const conn = flags['target-org'].getConnection(flags['api-version']); | ||
| const results = flags.plan | ||
| ? await importFromPlan(conn, flags.plan) | ||
| : await importFromFiles(conn, flags.files ?? []); | ||
|
|
||
| this.table({ | ||
| data: results, | ||
| columns: [ | ||
| { key: 'refId', name: 'Reference ID' }, | ||
| { key: 'type', name: 'Type' }, | ||
| { key: 'id', name: 'ID' }, | ||
| ], | ||
| title: 'Import Results', | ||
| }); | ||
| return results; | ||
| try { | ||
| const results = flags.plan | ||
| ? await importFromPlan(conn, flags.plan) | ||
| : await importFromFiles(conn, flags.files ?? []); | ||
|
|
||
| this.table({ | ||
| data: results, | ||
| columns: [ | ||
| { key: 'refId', name: 'Reference ID' }, | ||
| { key: 'type', name: 'Type' }, | ||
| { key: 'id', name: 'ID' }, | ||
| ], | ||
| title: 'Import Results', | ||
| }); | ||
| return results; | ||
| } catch (err) { | ||
| const error = err as Error; | ||
| if ( | ||
| error.cause && | ||
| error.cause instanceof Error && | ||
| 'data' in error.cause && | ||
| isObject(error.cause.data) && | ||
| 'message' in error.cause.data | ||
| ) { | ||
| const getTreeResponse = (payload: string): TreeResponse => { | ||
| try { | ||
| return JSON.parse(payload) as TreeResponse; | ||
| } catch (parseErr) { | ||
| // throw original error on invalid JSON payload | ||
| if (parseErr instanceof Error && parseErr.name === 'SyntaxError') { | ||
| throw error; | ||
| } | ||
| throw parseErr; | ||
| } | ||
| }; | ||
| const errorData = getTreeResponse(ensureString(error.cause.data.message)); | ||
|
|
||
| if (errorData.hasErrors) { | ||
| const errorResults = errorData.results | ||
| .map((result) => | ||
| result.errors.map((errors) => ({ | ||
| referenceId: result.referenceId, | ||
| StatusCode: errors.statusCode, | ||
| Message: errors.message, | ||
| fields: errors.fields.join(', ') || 'N/A', | ||
| })) | ||
| ) | ||
| .flat(); | ||
| this.table({ | ||
| data: errorResults, | ||
| columns: [ | ||
| { key: 'referenceId', name: 'Reference ID' }, | ||
| { key: 'StatusCode', name: 'Status Code' }, | ||
| { key: 'Message', name: 'Error Message' }, | ||
| { key: 'fields', name: 'Fields' }, | ||
| ], | ||
| title: 'Tree import errors', | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we manually report the errors in the table we need to manually set the exit code to 1 here: |
||
| const errors = new SfError('Data Import failed'); | ||
| errors.setData(errorResults); | ||
| throw errors; | ||
| } | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.