| .. | .. |
|---|
| 1 | 1 | |
|---|
| 2 | | -import {Component, Input} from '@angular/core'; |
|---|
| 2 | +import { Component, Injectable, Input } from '@angular/core'; |
|---|
| 3 | 3 | import {LocaleService} from './i18n'; |
|---|
| 4 | +import { Observable } from 'rxjs/Observable'; |
|---|
| 4 | 5 | |
|---|
| 5 | | -@Component({ |
|---|
| 6 | | - selector: 'field-readonly', |
|---|
| 7 | | - template: ` |
|---|
| 8 | | - <div layout="column" class="mat-input-container readonly" > |
|---|
| 9 | | - <div class="mat-input-wrapper"> |
|---|
| 10 | | - <div class="mat-input-table"> |
|---|
| 11 | | - <div class="mat-input-prefix"></div> |
|---|
| 12 | | - <div class="mat-input-infix"> |
|---|
| 13 | | - <div class="label-value mat-input-element">{{value}}</div> |
|---|
| 14 | | - <span class="mat-input-placeholder-wrapper" > |
|---|
| 15 | | - <label class="mat-input-placeholder mat-float"> |
|---|
| 16 | | - <span class="placeholder">{{label}}</span> |
|---|
| 17 | | - </label> |
|---|
| 18 | | - </span> |
|---|
| 19 | | - </div> |
|---|
| 20 | | - <div class="mat-input-suffix"></div> |
|---|
| 21 | | - </div> |
|---|
| 22 | | - <div class="mat-input-underline"></div> |
|---|
| 23 | | - </div> |
|---|
| 24 | | - </div>`, |
|---|
| 25 | | - styles: [`.readonly .mat-input-element { |
|---|
| 26 | | - margin-top: 0px !important; |
|---|
| 27 | | - color: rgba(0, 0, 0, 0.50); |
|---|
| 28 | | - }`, |
|---|
| 29 | | - `.readonly .mat-input-element { |
|---|
| 30 | | - margin-top: 0px; |
|---|
| 31 | | - color: rgba(0, 0, 0, 0.50); |
|---|
| 32 | | - }`, |
|---|
| 33 | | - `.readonly.mat-input-container { |
|---|
| 34 | | - width: 100%; |
|---|
| 35 | | - }`] |
|---|
| 36 | | -}) |
|---|
| 37 | | -export class FieldReadonlyComponent { |
|---|
| 38 | | - @Input('value') value: any; |
|---|
| 39 | 6 | |
|---|
| 40 | | - private _label : string; |
|---|
| 41 | | - @Input('label') |
|---|
| 42 | | - set label(txt: string) { |
|---|
| 43 | | - this._label = this.$L.get(txt); |
|---|
| 44 | | - } |
|---|
| 45 | | - get label(): string { return this._label; } |
|---|
| 46 | 7 | |
|---|
| 47 | | - constructor(private $L : LocaleService) { |
|---|
| 8 | +export interface IError { |
|---|
| 9 | + code: string | number, |
|---|
| 10 | + message: string, |
|---|
| 11 | + httpCode?: number |
|---|
| 12 | +} |
|---|
| 48 | 13 | |
|---|
| 49 | | - } |
|---|
| 14 | +export const DEFAULT_APP_ERROR_STATUS_CODE = 418; |
|---|
| 15 | +export const ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG"; |
|---|
| 16 | +export const ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE"; |
|---|
| 17 | + |
|---|
| 18 | +export class ErrorCodes { |
|---|
| 19 | + public static UNEXPECTED_ERROR = 1000; |
|---|
| 20 | + public static INVALID_CREDENTIALS = 1001; |
|---|
| 21 | + public static UNAUTHORIZED_ACCESS = 1002; |
|---|
| 22 | + public static NOT_FOUND = 1003; |
|---|
| 23 | + public static INVALID_FORMAT = 1004; |
|---|
| 24 | + public static WRONG_STATUS = 1005; |
|---|
| 25 | + |
|---|
| 26 | + public static INVALID_LICENSE_REQUEST_DATA = 1100; |
|---|
| 27 | + public static LICENSE_NOT_READY_FOR_RENEW = 1101; |
|---|
| 28 | + public static LICENSE_DATA_IS_NOT_VALID = 1102; |
|---|
| 29 | + public static LICENSE_IS_EXPIRED = 1103; |
|---|
| 30 | + |
|---|
| 31 | + public static INVALID_REQUEST_DATA = 1201; |
|---|
| 32 | + public static INVALID_REQUEST_DATA_FORMAT = 1202; |
|---|
| 33 | + public static BLOCKED_REQUEST_DATA = 1203; |
|---|
| 34 | + public static DUPLICATED_REQUEST_DATA = 1204; |
|---|
| 35 | + public static NO_AVAILABLE_LICENSES = 1205; |
|---|
| 36 | + |
|---|
| 37 | + public static INVALID_DATA = 1301; |
|---|
| 38 | +} |
|---|
| 39 | + |
|---|
| 40 | +@Injectable() |
|---|
| 41 | +export class BasicService { |
|---|
| 42 | + |
|---|
| 43 | + constructor(protected $L: LocaleService) {} |
|---|
| 44 | + |
|---|
| 45 | + public processErrorResponse(errorResponse: Response | any) { |
|---|
| 46 | + // In a real world app, we might use a remote logging infrastructure |
|---|
| 47 | + var error: IError = <IError>{}; |
|---|
| 48 | + if (errorResponse instanceof Response) { |
|---|
| 49 | + error.httpCode = errorResponse.status; |
|---|
| 50 | + } |
|---|
| 51 | + |
|---|
| 52 | + error.code = errorResponse.headers.get(ERROR_CODE_MESSAGE_HEADER) || error.httpCode; |
|---|
| 53 | + |
|---|
| 54 | + if (errorResponse.status === 403 /* forbidden */ || errorResponse.status === 401 /* unauthorized */) { |
|---|
| 55 | + error.message = this.$L.get('Invalid credentials'); |
|---|
| 56 | + error.code = ErrorCodes.INVALID_CREDENTIALS; |
|---|
| 57 | + } else if (errorResponse.status === 418 /* Teapot */) { |
|---|
| 58 | + error.message = errorResponse.headers.get(ERROR_MESSAGE_HEADER) || errorResponse.statusText || this.$L.get('Unknown'); |
|---|
| 59 | + } else { |
|---|
| 60 | + error.message = this.$L.get(`Unexpected error HTTP (${error.httpCode}) accessing to server. Contact with the administrator.`); |
|---|
| 61 | + } |
|---|
| 62 | + |
|---|
| 63 | + return Observable.throw(error); |
|---|
| 64 | + } |
|---|
| 50 | 65 | } |
|---|