Skip to content

Commit

Permalink
Add constants for common errors (#309)
Browse files Browse the repository at this point in the history
* Add constants for common errors

* Rename to `descopeErrors`
  • Loading branch information
itaihanski authored Dec 26, 2023
1 parent f2140ac commit a3ce8d2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ If you're performing end-to-end testing, check out the [Utils for your end to en

---

## Error Handling

Every `async` operation may fail. In case it does, there will be information regarding what happened on the response object.
A typical case of error handling might look something like:

```ts
import { SdkResponse, descopeErrors } from '@descope/node-sdk';

// ...

try {
const resp = await sdk.otp.signIn.email(loginId);
if (resp.error) {
switch (resp.error.errorCode) {
case descopeErrors.userNotFound:
// Handle specifically
break;
default:
// Handle generally
// `resp.error` will contain `errorCode`, `errorDescription` and sometimes `errorMessage` to
// help understand what went wrong. See SdkResponse for more information.
}
}
} catch (e) {
// Handle technical error
}
```

---

### OTP Authentication

Send a user a one-time password (OTP) using your preferred delivery method (_email / SMS_). An email address or phone number must be provided accordingly.
Expand Down
12 changes: 12 additions & 0 deletions lib/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line import/prefer-default-export
/** Common Error Codes */
export const descopeErrors = {
badRequest: 'E011001',
missingArguments: 'E011002',
invalidRequest: 'E011003',
invalidArguments: 'E011004',
wrongOTPCode: 'E061102',
tooManyOTPAttempts: 'E061103',
enchantedLinkPending: 'E062503',
userNotFound: 'E062108',
};
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,4 @@ export type {
JWTResponse,
} from '@descope/core-js-sdk';
export type { AuthenticationInfo };
export { descopeErrors } from './errors';
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default [
file: packageJson.main,
format: 'cjs',
sourcemap: true,
exports: 'default',
exports: 'auto',
},
plugins,
external,
Expand Down

0 comments on commit a3ce8d2

Please sign in to comment.