Skip to content

Commit

Permalink
Handle error and await for finished promises (#7)
Browse files Browse the repository at this point in the history
* Use machine translation workflow when requested

Add optional `isWorkflowMT` boolean param to `createJobBatch` method.
If `true`, pass `localeWorkflows` param with POST request, containing
an array with an object for each locale that has the locale id string
and the workflowUid for machine testing workflow in the AT project.
This param is not necessary if we want to keep the default workflow for
each locale, so only include if `isWorkflowMT` is true.

I couldn't find a way to fetch the workflowUids from the API, but they
can be found on the project dashboard under Project Settings > Workflows,
then clicking "Edit workflow Settings." Looks like uid is the same
across an account/project, so we _can_ use the same one, but I left it
out of this commit in case it should be kept private.
Next iteration will include updating the translations tab config to
include `options`, which could either accept one uid for MT or possibly
and array of workflow names + id's to allow selecting from a custom set
of workflows. This way we can set the ID from each project, instead of
in the plugin.

* Use workflowUid to create task with workflow

Along with updates to translations-tab, use an optional workflowUid (if
present) passed in by the user to set the workflow on task creation,
rather than hard-coding the specific uid string for the machine
translation workflow.

Dev can pass an optional array of `workflowOptions` in the options
config that's sent to `TranslationsTab`, enabling a select option in
the tab for choosing a workflow and passing in `workflowUid` with the
`createTask` params here.

Example:
```
const myCustomConfig = {
  ...defaultFieldLevelConfig,
  workflowOptions: [
    { workflowUid: '35cdc9b707d9', workflowName: 'Machine Translation (testing)' }
  ]
}
```

* Handle errors; await for finished promises

There are lots of points of potential silent and/or unhandled errors
due to not applying await for async functions and/or not using a
`catch` for any errors thrown. If we don't add `await` or `throw`
errors, then the translation tab won't be able to tell if a the call
is actually complete or was successful, resulting in a "Success" toast
that's potentially inaccurate.

I tried manually adding in errors in all the various calls and
triggering the "Import" from the button in the translation tab and this
update (along with the parallel change to `TranslationsTab`) allowed
all of them to be caught in the `try/catch` added to `imortFile` in
TranslationsTab.

Note, however, that I didn't test the documentLevel functions because I
don't have that implemented. I copied the changes made for fieldLevel
and have no reason to believe they wouldn't work :)

* Remove unnecessary catches

* Update package version

* Add `test` script

Adds `test` script so that added workflow doesn't fail, although there
aren't any tests yet

* Comment out test from github workflow

Commenting out `Test` from workflow for now because we don't have any
tests so it fails. Tried to start by adding a simple test, but everything
here is involves fetching from Sanity or Smartling so no simple functions
to add quickly.

* Remove `@sanity/client` dependency

Had added this as a dependency in attempt to fix error showing in VSCode
in helper.js where we import `sanityClient`, but that created different
issues when I tried to use `@sanity/client` instead of
`'part:@sanity/base/client'`, so just removing the dependency from package.
  • Loading branch information
apennell authored Feb 23, 2022
1 parent 7646f50 commit 718950f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- name: Lint
run: yarn lint

- name: Test
run: yarn test --ci --coverage --maxWorkers=2
# - name: Test
# run: yarn test --ci --coverage --maxWorkers=2

- name: Build
run: yarn build
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.4.0",
"version": "1.4.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand All @@ -13,6 +13,7 @@
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint src",
"prepare": "tsdx build",
"size": "size-limit",
Expand Down
11 changes: 10 additions & 1 deletion src/adapter/getTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export const getTranslation = async (
headers: getHeaders(url, accessToken),
})
.then(res => res.json())
.then(res => res.body)
.then(res => {
if (res.body) {
return res.body
} else if (res.response.errors) {
const errMsg =
res.response.errors[0]?.message ||
'Error retrieving translation from Smartling'
throw new Error(errMsg)
}
})

return translatedHTML
}
8 changes: 6 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const findDocumentAtRevision = async (
.then(req => req.json())
.then(req => req.documents[0])
}

return revisionDoc
}

Expand Down Expand Up @@ -74,7 +75,8 @@ export const documentLevelPatch = async (
cleanedMerge[key] = value
}
})
client

await client
.transaction()
//@ts-ignore
.patch(i18nDoc._id, p => p.set(cleanedMerge))
Expand Down Expand Up @@ -103,13 +105,15 @@ export const fieldLevelPatch = async (
} else {
baseDoc = await findLatestDraft(documentId)
}

const merged = BaseDocumentMerger.fieldLevelMerge(
translatedFields,
baseDoc,
localeId,
'en'
)
client

await client
.patch(baseDoc._id)
.set(merged)
.commit()
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const defaultDocumentLevelConfig = {
serialized.name = id
return serialized
},
importTranslation: (id: string, localeId: string, document: string) => {
importTranslation: async (id: string, localeId: string, document: string) => {
const deserialized = BaseDocumentDeserializer.deserializeDocument(
document
) as SanityDocument
documentLevelPatch(id, deserialized, localeId)
await documentLevelPatch(id, deserialized, localeId)
},
adapter: SmartlingAdapter,
}
Expand All @@ -35,11 +35,11 @@ const defaultFieldLevelConfig = {
serialized.name = id
return serialized
},
importTranslation: (id: string, localeId: string, document: string) => {
importTranslation: async (id: string, localeId: string, document: string) => {
const deserialized = BaseDocumentDeserializer.deserializeDocument(
document
) as SanityDocument
fieldLevelPatch(id, deserialized, localeId)
await fieldLevelPatch(id, deserialized, localeId)
},
adapter: SmartlingAdapter,
}
Expand Down

0 comments on commit 718950f

Please sign in to comment.